in reply to fun(?) with +=
Your problem is with the line
my $foo = shift || 1;Consider what happens if you give this program a command line argument of 0.
"shift" returns the value of the first command line argument - which is 0, therefore "shift || 1" will be 1.
You need to think about the difference between the truth value of a variable and it's defined value. You want to check that your command line argument is defined - not that it is true.
You probably want something like this:
$foo = shift; $foo = 1 unless defined $foo;
In Perl 5.10 you'll be able to use the "defined or" test to make this code easier.
$foo = shift // 1;
"The first rule of Perl club is you do not talk about
Perl club."
-- Chip Salzenberg
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: fun(?) with +=
by Anonymous Monk on Jan 03, 2005 at 12:26 UTC | |
by Rhose (Priest) on Jan 03, 2005 at 16:35 UTC |