in reply to Generic/Variable/Dynamic Subroutines? (Not a redefine)

Haukex and shmem already gave you the right answer, but I think it might be of interest to explain what happens

for my $meth (qw/ WriteLine Select Add /) { my $sub = sub { my $self = shift; my $it = $self->OStream->$meth($_[0]); $self->Trace(_CallerStr($it)); return $it; }; no strict 'refs'; *$meth = $sub; }

In this loop $meth and $sub form a so called closure, each instance of $sub is "closing over" a new instance of the lexical var $meth with a string.

This my $it = $self->OStream->$meth($_[0]); takes advantage of the fact that method calls are (contrary to sub calls) late evaluated strings, i.e. resolved at runtime. (for completeness $meth could also be a code_ref but that's not relevant here)

This

no strict 'refs'; *$meth = $sub;

is assigning a code_ref in $sub to the symbol named in $meth via a so called *glob.

That's how the STASH (Symbol Table hASH) works, each varname in a package has a hash key and the value is a "typeglob" with 6 so called "slots". Assigning a code_ref fills the CODE-slot, you can also address other slots like SCALAR, HASH or ARRAY and so on.

This case showed how to fill the %main:: stash, other namespaces need to be provided explicitly.

no strict 'refs' was necessary because using symbolic references like in *$meth (i.e. $meth is a string not a pointer like reference) is forbidden under strict.

This would work right away ...

*pack::name = sub {};

... but your name has to be dynamic.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^2: Generic/Variable/Dynamic Subroutines? (Not a redefine)
by mlewando (Initiate) on Aug 21, 2017 at 17:10 UTC
    Thanks for the explanation, I am relatively new to perl and my initial project was only reworking/enhancing existing code. Would you be able to elaborate, or comment, on the benefits of using autosplit for autoload, in that, placing any functions to be called by autoload after __END__ and having autosplit generate the *.al files necessary versus just having the functions inside of the package and having the appropriate method called during autoload? I was able to get both styles detailed, by shmem and Haukex, functional but didn't see any benefit from generating & using the *.al files, but found the simplest implementation was that detailed by shmem.
      > Would you be able to elaborate, or comment, on the benefits of using autosplit for autoload

      If you are loading code from a slow disk autosplit is faster.

      This used to be helpful 20 years ago. ...

      Update

      If compiling Perl code is slow, just autoloading on demand is faster

      Again, this used to be helpful 20 years ago. ... ;)

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        If compiling Perl code is slow, only autoloading on demand is faster

        Again, this used to be helpful 20 years ago. ... ;)

        Well, it is still useful for... what was the buzzword again? ah. "Continuous Integration" 'tis named I guess, if you replace AutoLoader with AutoReloader (PerlMonks thread).
        (yes, shameless plug :)

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'