Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Use cases for 'sub Pckg::func { }' ?

by ikegami (Patriarch)
on Jul 31, 2020 at 14:00 UTC ( [id://11120142]=note: print w/replies, xml ) Need Help??


in reply to Use cases for 'sub Pckg::func { }' ?

What are the use cases of that pattern?

The pattern you are observing is that the package directive controls the package in which code is compiled. sub X::foo { pp(\@_) } is simply not an exception to that. Effort to provide a special behaviour for sub X::foo { pp(\@_) } was not spent.

Put differently,

sub X::foo { pp(\@_) }
is short for
BEGIN { *X::foo = sub { pp(\@_) }; }
which is effectively what happens every time you import a symbol from a module (e.g. use X qw( foo );).

Replies are listed 'Best First'.
Re^2: Use cases for 'sub Pckg::func { }' ?
by LanX (Saint) on Jul 31, 2020 at 14:32 UTC
    You effectively just repeated what I said without answering my question.

    What are the use-cases where this idiomatic construct is useful?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      I answered that. It's used every time you export something. e.g. use X qw( foo ); It allows foo to find other subs it should find and to use use vars vars it expects to find.

        Well, I'm asking about use cases for the syntax and you are describing the semantics .

        The exporter will certainly not apply a

        sub X::foo {Block}

        syntax.

        TIMTOWTDI, but in which circumstances would I want to "do it this way"?

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11120142]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-16 20:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found