Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^2: How to assign string "0" in short circuit

by nikhil.patil (Sexton)
on Feb 05, 2008 at 06:46 UTC ( #666227=note: print w/replies, xml ) Need Help??

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:
sub function1 { my $var = defined $_[0] ? shift : 'No argument'; print "$var\n"; }
But I still wonder if we cannot assign string value "0" to a variable using short circuit.

// 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:
my $var = $hash1{$name} || $hash2{$name} || $some_default_value;
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.0

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
    Short circuit operators always evaluate in boolean context. In boolean context there are only true and false. The string "0" and numberic 0 are, among others, false. But, 0 (or empty string) is defined. So you need to check for defined-ness to allow them. See True or False? A Quick Reference Guide.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://666227]
[Corion]: GotToBTru: Heh ;) I'm not really endorsing it, as I found that any gains made through it are easily squandered by the hours spent on configuring it. At least in my case :)
[GotToBTru]: Like most Swiss Army Knives, you can cut yourself trying to find the blade you actually want
[GotToBTru]: i am satisfied with the default settings turning the seemingly random formatting into something reliable
[Corion]: GotToBTru: Yeah, I started a quick(ly implemented but slowly running) implementation of a program that would guess the "best" configuration (that is, least amount of changes) from my code, but then found that it stops parsing the source as soon as it ..
[GotToBTru]: sadly, it can't do anything about this
[Corion]: ... encounters sub foo($bar,$baz) {, which is something I like nowadays
[Corion]: GotToBTru: Yeah, that stuff is hard to do away automatically ;)
[GotToBTru]: yeah, you mentioned it could not handle signatures

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2017-02-27 14:32 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (387 votes). Check out past polls.