[tpm] Is there a three-way version of...

Mike Stok mike at stok.ca
Sat May 30 12:20:22 PDT 2009


On May 30, 2009, at 2:30 PM, Shaun Fryer wrote:

>>>        The "||", "//" and "&&" operators return the last value  
>>> evaluated
>>>        (unlike C’s "||" and "&&", which return 0 or 1).
>
>>    $foo = $opt{foo} || $ENV{PROG_FOO} || $rc_opts{foo} || 'default';
>
> This is interesting. I learned something new. Now I'm personally
> curious if there's an operator that instead of doing "or equals", does
> "equals or". For instance, I find myself doing this alot (particularly
> in accessors/setters)...
>
> $x = $y if defined $y;
>
> What would be helpful is to be able to do this instead.
>
> $x =|| $y;
>
> Or even better...
>
> $x =// $y;


Maybe

$x = $y // $x?

If that's in the context of a routine which acts as both a reader and  
writer of an attribute then it might be safer to count arguments so  
you can tell the difference between

   $obj->get_or_set();

and

   $obj->get_or_set(undef);

depending on context and circumstance, of course.

Mike

-- 

Mike Stok <mike at stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.






More information about the toronto-pm mailing list