Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Assignable Subroutines

by BrentDax (Hermit)
on Jan 26, 2005 at 01:38 UTC ( [id://425096]=note: print w/replies, xml ) Need Help??

in reply to Assignable Subroutines

Untested proof-of-concept. Run it only if you don't mind your hard disks being set on fire.

package Lvalue::GetSet; use Exporter; our @ISA=qw(Exporter); our @EXPORT=qw(get set); sub get(&@) :lvalue { unshift @_, 'get'; goto &getset; } sub set(&@) :lvalue { unshift @_, 'set'; goto &getset; } sub getset :lvalue { if(@_ == 2) { return @_; } elsif(@_ == 4) { my $var; tie $var, 'Lvalue::GetSet::Tie', @_; $var; } else { die "can't happen"; } } package Lvalue::GetSet::Tie; sub TIESCALAR { my($pkg, %obj)=@_; bless \%obj, $pkg; } sub FETCH { $_[0]->{get}->(); } sub STORE { $_[0]->{set}->($_[1]); }

Usage would be something like:

sub name { my $me=shift; get { $me->{name}; } set { die "Invalid name" unless $_[0] =~ /^(\w+)$/; $me->{name} = $1; } }

What's wrong with something like this?

--Brent Dax
There is no sig.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://425096]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-05-23 16:32 GMT
Find Nodes?
    Voting Booth?

    No recent polls found