Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Re: What does ' $subs{$arg}->($arg); ' mean?

Expand Messages
  • Tom Barron
    ... I think what you want is this: %subs = (key1 = value1, key2 = value2, address = [ &all_sub, element2 , New York , element4 ]); then print
    Message 1 of 6 , Dec 7, 2000
    • 0 Attachment
      Steve Milo wrote:
      >
      > Another question.
      >
      > Some changes to the structure as before.
      >
      > >
      > > %subs{
      > > key1=>value1,
      > > key2 => value2,
      > > address => ("element1", "element2", "New York", "element4")
      > > };
      > >
      >
      > >
      > > %subs(
      > > key1=>value1,
      > > key2 => value2,
      > > address => ("/&all_sub", "element2", "New York", "element4")
      > > );
      >
      > How can I get something like this to work?

      I think what you want is this:

      %subs = (key1 => value1,
      key2 => value2,
      address => [\&all_sub, "element2", "New York", "element4"]);

      then

      print $subs{key1}; # yields 'value1'
      print $subs{address}->[1]; # yields 'element2'
      $subs{address}->[0](%subs); # should call all_sub and pass in %subs

      > I would like to summon a subroutine and once it starts to execute I
      > want to pass the other variables to it for filtering purposes?

      I don't understand what you mean here. Are you using multithreading or
      multiple processes?
      --
      Tom
    • Steve Milo
      I ll play with this tomorrow, but what I m trying to accomplish is once I summon the subroutine I want to pass the other variables to use as a test case. Or
      Message 2 of 6 , Dec 7, 2000
      • 0 Attachment
        I'll play with this tomorrow, but what I'm trying to accomplish is
        once I summon the subroutine I want to pass the other variables to
        use
        as a test case. Or for example I would filter anything with New York
        in a file called element2.

        Steve M

        --- In perl-beginner@egroups.com, Tom Barron <tbarron@m...> wrote:
        > Steve Milo wrote:
        > >
        > > Another question.
        > >
        > > Some changes to the structure as before.
        > >
        > > >
        > > > %subs{
        > > > key1=>value1,
        > > > key2 => value2,
        > > > address => ("element1", "element2", "New York", "element4")
        > > > };
        > > >
        > >
        > > >
        > > > %subs(
        > > > key1=>value1,
        > > > key2 => value2,
        > > > address => ("/&all_sub", "element2", "New York", "element4")
        > > > );
        > >
        > > How can I get something like this to work?
        >
        > I think what you want is this:
        >
        > %subs = (key1 => value1,
        > key2 => value2,
        > address => [\&all_sub, "element2", "New York",
        "element4"]);
        >
        > then
        >
        > print $subs{key1}; # yields 'value1'
        > print $subs{address}->[1]; # yields 'element2'
        > $subs{address}->[0](%subs); # should call all_sub and pass in
        %subs
        >
        > > I would like to summon a subroutine and once it starts to execute
        I
        > > want to pass the other variables to it for filtering purposes?
        >
        > I don't understand what you mean here. Are you using multithreading
        or
        > multiple processes?
        > --
        > Tom
      • Daniel Jones
        On Fri, 08 Dec 2000 04:21:26 -0000, Steve Milo ... What do you mean by once I summon the subroutine...? I ve never heard the term summon used in
        Message 3 of 6 , Dec 8, 2000
        • 0 Attachment
          On Fri, 08 Dec 2000 04:21:26 -0000, "Steve Milo"
          <slavik944@...> wrote:

          >I'll play with this tomorrow, but what I'm trying to accomplish is
          >once I summon the subroutine I want to pass the other variables to
          >use
          >as a test case. Or for example I would filter anything with New York
          >in a file called element2.

          What do you mean by "once I summon the subroutine...?"
          I've never heard the term "summon" used in reference to a
          subroutine? Are you referring to executing it? If so, how
          do you intend to pass other variables to it after executing
          it?

          In general, execution of a perl script is linear. (That is,
          each line is executed one at a time. Perl does, of course,
          have looping and branching, generating multiple paths of
          execution through a script.) When you call a subroutine,
          control passes to that subroutine and doesn't return until
          the subroutine is finished. You pass variables when you
          initially call it, but you can't pass more variables while
          it's executing.

          You can use multithreading, of course. When you fork a
          program, two copies of the program are created and execute
          simultaneously. Each thread still executes linearly. You
          can communicate between the threads, so you could
          conceivably have one thread executing a subroutine and the
          other passing info to it. If this is what you're talking
          about, it seems a rather convoluted way to do things.

          You can also pipe data from one script to another. This
          wouldn't really be passing other variables to a subroutine
          either, but it's the only other thing I can think of that
          you might be talking about.

          Dan
        • Tom Barron
          Hi, Steve. I don t know of any way to (or any point in) passing information to a subroutine *after* it s been called unless your program is multi-threaded, or
          Message 4 of 6 , Dec 8, 2000
          • 0 Attachment
            Hi, Steve. I don't know of any way to (or any point in) passing
            information to a subroutine *after* it's been called unless your program
            is multi-threaded, or you've got multiple processes working together
            using inter-process communication.

            If you have only one flow of control (the normal situation in a
            non-threaded program), there's nobody to pass more information to the
            subroutine that's running, since the subroutine has control. If somebody
            else had control so they could pass more information to the sub, the sub
            would not be running.

            Subroutines can get information from the parameters passed to them (@_),
            or from global variables. Generally, passing parameters and avoiding
            global data makes for more modular, portable code.

            I'm still not at all clear on what you're trying to do. What do you mean
            by "a test case"?. When you say filter, do you mean you'd discard
            entries with New York or keep them and discard everything else? "in a
            file called element2" -- why is the file name important? Are you talking
            about a disk file or a collection of information in memory?

            Steve Milo wrote:
            >
            > I'll play with this tomorrow, but what I'm trying to accomplish is
            > once I summon the subroutine I want to pass the other variables to
            > use
            > as a test case. Or for example I would filter anything with New York
            > in a file called element2.
            --
            Tom
          • Steve Milo
            ... Yes, poor choice of words. ... I dont know I thought it was possible. %subs = (key1 = value1, key2 = value2, address = [ element2 , New York ,
            Message 5 of 6 , Dec 8, 2000
            • 0 Attachment
              > What do you mean by "once I summon the subroutine...?"
              > I've never heard the term "summon" used in reference to a
              > subroutine? Are you referring to executing it?

              Yes, poor choice of words.

              > If so, how
              > do you intend to pass other variables to it after executing
              > it?

              I dont know I thought it was possible.

              %subs = (key1 => value1,
              key2 => value2,
              address => ["element2", "New York", "element4", \&all_sub]);

              then

              print $subs{key1}; # yields 'value1'
              print $subs{address}->[1]; # yields 'element2'
              $subs{address}->[0](%subs); # should call all_sub and pass in %subs

              What I was hoping to accomplish was pass or declare the variables at
              the point when it is referenced to. I thought it would have been
              possible to declare my variables here then execute the subroutine.
              I may not have thought this through enough.


              Steve M
            • Andrew Johnson
              Steve wrote: ! I dont know I thought it was possible. ! ! %subs = (key1 = value1, ! key2 = value2, ! address = [ element2 , New York , element4 ,
              Message 6 of 6 , Dec 9, 2000
              • 0 Attachment
                Steve wrote:

                ! I dont know I thought it was possible.
                !
                ! %subs = (key1 => value1,
                ! key2 => value2,
                ! address => ["element2", "New York", "element4", \&all_sub]);
                !
                ! then
                !
                ! print $subs{key1}; # yields 'value1'
                ! print $subs{address}->[1]; # yields 'element2'

                That would print "New York" (arrays count from 0).

                ! $subs{address}->[0](%subs); # should call all_sub and pass in %subs

                The element in [0] of that array is not a sub ref, the element
                in [3] of that array is the sub ref, so you'd want:

                $subs{address}[3]->(%subs);

                Now all_sub will get the entire hash as an argument, so if
                you defined all_sub to be:

                sub all_sub {
                print "@_\n";
                }

                Then your output will be:

                key1 value1 address ARRAY(0x80e73c0) key2 value2

                (that is, you'll get each key and value and the value for
                the key 'address' is an array reference -- so you'd need
                to deal with all that appropriately inside of all_sub).

                regards,
                andrew

                --
                Andrew L. Johnson http://members.home.net/andrew-johnson/
                It may be that your sole purpose in life is simply to
                serve as a warning to others.
              Your message has been successfully submitted and would be delivered to recipients shortly.