Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: shift and logical or

by mhearse (Hermit)
on Nov 17, 2012 at 01:20 UTC ( #1004272=note: print w/ replies, xml ) Need Help??


in reply to shift and logical or

Consider

my $val ||= "undefined"

Which is equivalent to:

if (!$val) { $val = "undefined"; }

Blocks of code like that tend to clutter up your code.

Also consider:

$val = ($val) ? "defined" : "undefined";

Which is equivalent to:

if ($val) { $val = "defined"; } else { $val = "undefined"; }

Also, remember perl evaluates expressions for truth.


Comment on Re: shift and logical or
Select or Download Code
Re^2: shift and logical or
by Kenosis (Priest) on Nov 17, 2012 at 02:41 UTC

    Defined, undefined or false?

    use strict; use warnings; my $val = 0; print +($val) ? "defined" : "undefined"; # prints undefined print "\n"; print +(defined $val) ? "defined" : "undefined"; # prints defined undef $val; print "\n"; print +(defined $val) ? "defined" : "undefined"; # prints undefined

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004272]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2014-09-30 12:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (367 votes), past polls