Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Assignable Subroutines

by BrentDax (Hermit)
on Jan 26, 2005 at 01:38 UTC ( #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?

=cut
--Brent Dax
There is no sig.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2022-01-26 12:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (69 votes). Check out past polls.

    Notices?