in reply to Re: Uses for an lvalue subroutine
in thread Uses for an lvalue subroutine
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Uses for an lvalue subroutine
by chipmunk (Parson) on Jan 10, 2001 at 09:02 UTC | |
by tadman (Prior) on Jan 11, 2001 at 02:14 UTC |