in reply to on to better coding (no uninitialized values)
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: on to better coding (no uninitialized values)
by echo (Pilgrim) on Aug 23, 2001 at 21:16 UTC | |
|
(ichimunki) Re x 2: on to better coding (no uninitialized values)
by ichimunki (Priest) on Aug 23, 2001 at 23:56 UTC |