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

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

Expand Messages
  • Steve Milo
    Another question. Some changes to the structure as before. ... How can I get something like this to work? I would like to summon a subroutine and once it
    Message 1 of 4 , Dec 6, 2000
      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 would like to summon a subroutine and once it starts to execute I
      want to pass the other variables to it for filtering purposes?

      Thanks,
      Steve M
    • Andrew Johnson
      Maisha wrote: [ Subject: Re: [PBML] What does $subs{$arg}- ($arg); mean?] ! It means that you have a hash called subs, and in that hash, one of ! the
      Message 2 of 4 , Dec 9, 2000
        Maisha wrote:

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

        ! It means that you have a hash called subs, and in that hash, one of
        ! the values is a reference to an array. And you are calling one of
        ! the elements from the array.

        Actually, it means we have a hash named %subs and the values are
        references to subroutines and we are calling the sub reference
        associated with the key $arg and passing it the argument $arg
        as well.

        For example, let's take a ref to a subroutine and store it in
        a scalar and then call it with an argument:

        my $sub = \&foo;
        $sub->("Hello");

        sub foo {
        my $param = shift;
        print "$param World\n";
        }

        We can also store such a sub ref inside a hash:

        my %subs = ( Hello => \&foo,
        Bye => \&foo,
        );

        my $arg = "Hello";
        $subs{$arg}->($arg);

        $arg = "Bye";
        $subs{$arg}->($arg);

        sub foo {
        my $param = shift;
        print "$param World\n";
        }

        Hope that helps.

        andrew

        --
        Andrew L. Johnson http://members.home.net/andrew-johnson/
        Some people, when confronted with a problem, think 'I know,
        I'll use regular expressions.' Now they have two problems.
        -- Jamie Zawinski, on comp.lang.emacs
      Your message has been successfully submitted and would be delivered to recipients shortly.