develooper Front page | perl.perl6.language | Postings from June 2005

Re: AUTLOAD and $_

Thread Previous | Thread Next
From:
Juerd
Date:
June 20, 2005 03:11
Subject:
Re: AUTLOAD and $_
Message ID:
20050620101147.GV7412@c4.convolution.nl
Sam Vilain skribis 2005-06-20 12:54 (+1200):
>  sub AUTOLOAD($_ = $CALLER::$_, *@_) {
> In a way, $_ forms part of the prototype definition, but is "out of band"
> to the regular arguments on @_; it can't interfere with positional
> characteristics, or you have to "shift" it off before you goto the right
> sub.
(...)
> Perhaps to avoid that mess, the AUTOLOAD function is simply expected to
> call &func.goto if it wants all the effects of the presence of the
> AUTOLOAD sub to go away.  Assuming that the prototype is re-checked on a
> goto, to ensure that type guarantees specified in the function signature
> are honoured, then the necessary side effects should just happen.

I think there exists an even simpler way to avoid any mess involved.
Instead of letting AUTOLOAD receive and pass on arguments, and instead
of letting AUTOLOAD call the loaded sub, why not have AUTOLOAD do its
thing, and then have *perl* call the sub?

    sub AUTOLOAD ($whatever) {  # but no *@_
        my $s = get_subref_for $whatever;
        our &::($whatever) := $s;
        return $s;  # non-subref indicates failure
    }

If you want to load it again each time, remove the := line.

The line can be shortened to 

    sub AUTOLOAD ($w) { return our &::($w) = get_subref_for $w }

or just

    sub AUTOLOAD { our &::($^a) = get_subref_for $^a }

Re arguments: I think a single positional argument makes more sense than
requiring that its name be $_.


Juerd
-- 
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html 
http://convolution.nl/gajigu_juerd_n.html

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About