in reply to Re: Dynamically Calling a Subroutine
in thread Dynamically Calling a Subroutine

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.

Replies are listed 'Best First'.
Re^3: Dynamically Calling a Subroutine
by BrowserUk (Patriarch) on Jul 12, 2011 at 23:53 UTC

    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.
Re^3: Dynamically Calling a Subroutine
by pemungkah (Priest) on Jul 17, 2011 at 01:47 UTC
    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.