While waiting for the speculated "defined" version of "||", I wrote def -- Deal nicely with undefined values. That simplifies your second case: $foo = def( $bar, $default_for_foo ); Throwing together something for exists I came up with this: $a = has( a=>$hashref, $default_for_a ); I replaced $foo with $hashref in my example above.
Here is what has() looks like:
Petruchio has been talking about creating a module to provide fairly simple tools like this that allow you to replace idiomatic constructs with simpler (more natural) constructs. I'll think has() and def() are worth including. (: - tye (but my friends call me "Tye")sub has { my( $key, $hv, $val, $force )= @_; if( exists $hv->{$key} ) { $val= $hv->{$key}; } elsif( ! defined wantarray || $force ) { $hv->{$key}= $val; } return $val; }
In reply to (tye)Re: Effecient shortcut for ?:
by tye
in thread Effecient shortcut for ?:
by traveler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |