in reply to I'm looking for some 'heavy magic'
package Mutator; use overload '%{}'=>\&as_hash, '${}'=>\&as_scalar, '@{}'=>\&as_array; sub new{ my $class=shift; my $scalar; #To get be able to create a scalar ref. #This is where we store the real values. We use a closeure #below, so %ref will be associated with our anon. sub. #If you want to do some init of the variables, you can #do it here. my %ref=(hash=>{}, array=>[], scalar=>\$scalar, ); # Now this is the sub that returns the actual reference # that we use. We call it in the overloaded methods. my $sub=sub { my $arg=shift; return $ref{$arg}; }; #bless the anon. sub into this class return bless $sub, $class; } #Get hash ref sub as_hash{ my $s=shift; return $s->("hash"); } #Get scalar ref sub as_scalar{ my $s=shift; return $s->("scalar"); } #get array ref sub as_array{ my $s=shift; return $s->("array"); } 1;
An questions?
GoldClaw
|
|---|