Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

How to assign string "0" in short circuit

by nikhil.patil (Sexton)
on Feb 04, 2008 at 12:08 UTC ( #665938=perlquestion: print w/replies, xml ) Need Help??
nikhil.patil has asked for the wisdom of the Perl Monks concerning the following question:


In the following scenario:
sub function1 { $var = shift || "No argument"; print $var . "\n"; }
How do I make sure that a string value "0" is assigned to $var when the argument passed to the function is 0?

Currently, if 0 is passed to the function, it assigns "No argument" to the variable. I want "No argument" to be assigned only if there was no argument passed in the call to the function. Can I not use short circuit to do this??

Thanks in advance!

Replies are listed 'Best First'.
Re: How to assign string "0" in short circuit
by olus (Curate) on Feb 04, 2008 at 12:28 UTC
    sub function1 { my $var = (defined $_[0]) ? $_[0] : "No argument"; print $var . "\n"; }
Re: How to assign string "0" in short circuit
by poolpi (Hermit) on Feb 04, 2008 at 13:05 UTC
    sub function1 { my ($var) = @_? @_:'no arg'; print qq{$var\n}; } function1(0); function1('0'); function1('zero'); function1();
    Output: 0 0 zero no arg


    Update : 'no arg'
Re: How to assign string "0" in short circuit
by BrowserUk (Pope) on Feb 04, 2008 at 13:07 UTC
Re: How to assign string "0" in short circuit
by jwkrahn (Monsignor) on Feb 04, 2008 at 12:23 UTC
    Upgrade to Perl 5.10.0 and use the  // operator instead.
Re: How to assign string "0" in short circuit
by saberworks (Curate) on Feb 05, 2008 at 04:56 UTC
    None of the code shown so far matches the original. When you shift off @_, it actually removes the value, allowing you to use the rest of @_ at your leisure.
    #!/usr/bin/perl use warnings; use strict; sub function1 { my $var = defined $_[0] ? shift : 'No argument'; print "$var\n"; } function1('zero'); function1(0); function1('0'); function1(0e0); function1('foolio'); function1();
      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
        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: perlquestion [id://665938]
Approved by citromatik
Front-paged by citromatik
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2017-10-21 05:07 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (269 votes). Check out past polls.