Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Perl5 Language Extension: Definedness-Triggered Shortcut Operators

by tobyink (Canon)
on Mar 17, 2012 at 13:04 UTC ( [id://960169]=note: print w/replies, xml ) Need Help??


in reply to RFC: Perl5 Language Extension: Definedness-Triggered Shortcut Operators

For defined-or we already have //.

As far as the other two go, take a look at PerlX::Perform which I believe should allow you to do defined-and and defined-dereference, albeit with more verbose syntax.

For example, imagine that $bob->spouse might return a Person object, or might return undef if Bob isn't married. Then PerlX::Perform allows:

$bobs_wifes_name = perform { $_->name } wherever $bob->spouse;

If the wherever expression evaluates to undefined, then the perform keyword returns undef (or the empty list if called in list context). If the wherever expression evaluates to a defined value (even if that value is false) then that value is assigned to $_ and the perform block is triggered.

PerlX::Perform also offers an alternative syntax which works more or less the same way. Note that the alternative syntax uses a comma, whereas the syntax above does not.

$bobs_wifes_name = wherever $bob->spouse, perform { $_->name };

So your A :&& B can be written as:

wherever A, perform {B}

And your A:->B can be written as:

wherever A, perform {$_->B}

Do give PerlX::Perform a try. Patches for improved performance, or more sugary syntax are likely to be accepted.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found