in reply to Re: How to assign string "0" in short circuit
in thread How to assign string "0" in short circuit
Thank you all for your suggestions. Finally I used the following code to solve the problem:
// works well, but that is in Perl 5.10.0. What if we have a situation like the following (in Perl 5.8.8), where we have multiple expressions involved in the short circuit in order of preference:
But I still wonder if we cannot assign string value "0" to a variable using short circuit.sub function1 { my $var = defined $_[0] ? shift : 'No argument'; print "$var\n"; }
// works well, but that is in Perl 5.10.0. What if we have a situation like the following (in Perl 5.8.8), where we have multiple expressions involved in the short circuit in order of preference:
This will assign the default value to $var if say the key $name is not defined in %hash1 and is assigned a value 0 in %hash2. Is a if-elsif ladder or complex nested ternary operators the only way to solve this? Or may be this is the reason why they came up with // in Perl 5.10.0my $var = $hash1{$name} || $hash2{$name} || $some_default_value;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: How to assign string "0" in short circuit
by naikonta (Curate) on Feb 05, 2008 at 07:52 UTC |
In Section
Seekers of Perl Wisdom