I never noticed before that in order to use the method as an lvalue, you had to use it as an rvalue first to define the function. But you can define AUTOLOAD as an lvalue method, and then it almost works, as the following code demonstrates:
#!/usr/bin/perl
package Foo;
sub new { bless {}, shift }
sub DESTROY {1}
sub AUTOLOAD : lvalue {
my ( $method ) = $AUTOLOAD =~ /^.*::(.+)$/
or die "Invalid call to $AUTOLOAD";
*$AUTOLOAD = sub : lvalue {
my $self = shift;
if (@_) {
$self->{$method} = shift;
return $self;
}
$self->{$method};
};
goto &$AUTOLOAD;
# Uncomment the following 'useless' line
# and it works:
# $Foo::baz;
}
package main;
my $foo = Foo->new;
$foo->bar = 4;
print $foo->bar,"\n";
With the above code you get this error:
Can't modify goto in lvalue subroutine return at ...
If you uncomment the last line of the AUTOLOAD, you seem to
fool the compiler into thinking that AUTOLOAD is going to return a valid lvalue, even though it never actually returns that value, since you
goto somewhere else first.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.