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