in reply to Re: Experimenting with Lvalue Subs
in thread Experimenting with Lvalue Subs
At first I thought it must be a source filter then I noticed that it used XS and stopped looking. It is neat but not practical IMO.#!/usr/bin/perl use strict; use warnings; use Want; my %obj; sub cool :lvalue { my $key = shift; my %ok = map { $_ => 1 } 1..7; my $junk; if ( want( 'LVALUE ASSIGN' ) ) { if ( ! $ok{ $key } ) { warn "$key is not valid"; lnoreturn; } else { my ($rval) = want( 'ASSIGN' ); if ( ! $ok{ $rval } ) { warn "$rval as rval is not valid"; lnoreturn; } $obj{ $key } = $rval; } lnoreturn; } elsif ( want( 'RVALUE' ) ) { if ( ! $ok{ $key } ) { warn "$key is not valid"; rreturn $junk; } else { rreturn $obj{ $key }; } } else { warn "Houston, we have a problem"; } return; } cool( 1 ) = 3; print cool( 1 ), "\n"; cool( 5 ) = 42; cool( 9 ) = 3; print "$_ : $obj{$_}\n" for keys %obj; __END__ 3 42 as rval is not valid at foo.pl line 20. 9 is not valid at foo.pl line 14. 1 : 3
Cheers - L~R
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Experimenting with Lvalue Subs
by runrig (Abbot) on Jan 25, 2005 at 00:58 UTC | |
by Limbic~Region (Chancellor) on Jan 25, 2005 at 14:01 UTC |