Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Experimenting with Lvalue Subs

by Limbic~Region (Chancellor)
on Jan 25, 2005 at 00:09 UTC ( [id://424750]=note: print w/replies, xml ) Need Help??


in reply to Re: Experimenting with Lvalue Subs
in thread Experimenting with Lvalue Subs

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.

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
    To me the only clumsy thing is that you have to supply those ugly arguments to the want() function. It might be nicer to have something like is_rvalue() and is_lvalue() aliases to get to that functionality. I'm not sure what else you could do to make it less clumsy, did you have anything else in mind?
      runrig,
      To me the only clumsy thing is that you have to supply those ugly arguments

      In assignment, I would have assumed you could return a variable that was going to get assigned to but couldn't figure out how to do this myself. Apparently, lnoreturn needs to be on a line all by itself so you are forced to do the assignment yourself inside the sub. Perhaps this just takes some getting used to.

      If I were going to dream up some syntax, I would create a new predefined variable - let's call it %LVAL. It would need to be localized and bound to the scope of the currently executed sub, much like $| is bound to the currently selected filehandle. It would have keys you could use to get at the things you wanted. Perhaps you could even define additional code refs during sub declaration for your validation routine, and any pre/post processing you wanted to do that would be handled for you automagically.

      In any account, I have accomplished what I had hoped to. People are talking about it. Not just in a p5 context, but also in a p6 context. This is a good thing. It sounds like some feel that the current p6 design isn't much better than the p5.

      Cheers - L~R

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-23 07:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found