in reply to Dynamically Calling a Subroutine

Globbing:

sub update_test{ print 'hi' };; $n = 'test';; *{'update_' . $n }->();; hi

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Dynamically Calling a Subroutine
by Anonymous Monk on Jul 12, 2011 at 23:39 UTC
    Thanks. It works! But I want to understand why it works.

    Can you explain what is happening in this line?
    *{'update_' . $n }->();

    I thought glob is only used for directories.

      See Globject sigils.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      Remember that perl allows you to have the same name for different things: $foo, @foo, %foo, and foo() (the subroutine 'foo') are all stored in different slots of the symbol table entry for the key 'foo': $foo is in SCALAR, @foo in ARRAY, %foo in HASH, and foo() in the CODE slot. We reference all of these things at once via a "glob".

      The string inside the curlies is the key we'll use to access the symbol table.

      The *{...} (the glob reference syntax) is essentially saying "find the symbol table entry that contains references to all the things with this name".

      The ->() following the dereference says "call what the CODE slot references in this entry." It the sub had arguments, you could put them in the parens.