package Object;
use vars '$AUTOLOAD';
# :
sub AUTOLOAD : lvalue
{
my ($self) = shift;
$AUTOLOAD =~ s/.*://;
@_? $self->{$AUTOLOAD} = shift : $self->{$AUTOLOAD} ||= undef;
}
Sweet candy.
Now you can do stuff like this:
my ($object) = new Object;
$object->whatever = "Data";
print "Whatever is '",$object->whatever,"'\n";
It "goes both ways" as an LVALUE, and this AUTOLOAD routine
has backwards compatibility with the older, somesay wacky
function method:
my ($object) = new Object;
$object->whatever("Data");
print "Whatever is '",$object->whatever(),"'\n";
You even get warnings with perl -w if you use
uninitialized members.
In reply to Re^2: Uses for an lvalue subroutine
by tadman
in thread Uses for an lvalue subroutine
by bent
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |