runrig,
I just spent the better part of an hour playing with
Want. Indeed, it can do what I was suggesting albeit very clumsily.
#!/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
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.