If $file is defined, no further action is taken, since || short-circuits out on the true value. Otherwise, it sets $file to the default name given. Any time further, there's very little chance that $file will be undef'd.sub do_something { my ( $file ) = @_; $file ||= "default.txt"; ... }
The only time this doesn't work is if a value 0 or the empty string ('') is passed, since these also evaluate to false. In cases where these are possible values, I then do defer to defined to make sure that the values aren't overwritten.
-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
It's not what you know, but knowing how to find it if you don't know that's important
In reply to Re: on to better coding (no uninitialized values)
by Masem
in thread on to better coding (no uninitialized values)
by melguin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |