addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscontroller-playcredit-cardcrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobe--smallglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1launch-new-window--smalllight-bulblinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonprintShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

Re: [perl-108] AUTOLOAD and late binding? match vars? params

From: Graham K.
Sent on: Thursday, July 24, 2014 5:43 PM
A couple solutions:

The first is that regex match variables are implicitly localized to
where the match is done.  You can take advantage of this by wrapping
the matching you do on $AUTOLOAD in a block:
sub AUTOLOAD {
    my $func = $AUTOLOAD;
    my $space;
    {
        if(!($func =~ /(.*)::(.*)/)) {
            die("Autoload: parse of $func failed\n");
        }
        $space = $1;
        $func = $2;
    }
    ...
}

The second is to make a copy of the incoming arguments before doing
any matching, and using that array instead of @_ when calling the next
sub.  This will break the aliasing in the arguments so changes to $1
won't impact the values:
sub AUTOLOAD {
    my @args = @_;
    ...
    return(eval("$func(\­@args)"));
}

On Thu, Jul 24, 2014 at 6:10 PM, Andy Bach <[address removed]> wrote:
> We have app that overloads Autoload func to use a lookup list for commonly
> used subs that may not have been require-ed in yet.  It's a little byzantine
> but, if needed, when Perl hits a unknown function call, the AUTOLOAD
> function either parses a file (and creates an in-memory list) or checks the
> list for the function name; if found, it evals a require of the lib file and
> then eval the function call w/ the pass-ed in params.
>                 eval ("package $space;require \"$tNode->{FileNa­me}\";");
>                 return(eval("$func(\­@_)"));
>
> For instance
> my $clean_v = Meta($filename);
>
> Meta() gets found, it's lib "MetaLib.pl" required and Meta gets called with
> $filename as the param. It turns out to be a little weird if the call uses a
> match var:
> my $clean_v = Meta($1);
>
> as our AUTOLOAD script does:
>         my $func = $AUTOLOAD;
>         if(!($func =~ /(.*)::(.*)/)) {
>                 die("Autoload: parse of $func failed\n");
>         }
>         my $space = $1;
>         $func = $2;
>
> Before the match, @_ (and $1) in AUTOLOAD have the correct value from the
> original match, (wrap the if { ... }  with
> warn("aut: @_ $1 \n");
>
> and I see:
> aut: mo mo
> aut: main main
> ) but afterwards, it's become the namespace matched in the if.  Of course,
> this only happens if the first use of the autoloaded function happens to use
> a match var - took a looooong time to debug that one.
>
> --
>
> a
>
> Andy Bach,
> [address removed]
>[masked] cell
>[masked] wk
>
>
>
>
> --
> Please Note: If you hit "REPLY", your message will be sent to everyone on
> this mailing list ([address removed])
> This message was sent by Andy Bach ([address removed]) from MadMongers
> (Madison Perl Mongers).
> To learn more about Andy Bach, visit his/her member profile
> To report this message or block the sender, please click here
> Set my mailing list to email me As they are sent | In one daily email |
> Don't send me mailing list messages
>
> Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy