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

Re: Surprising result when using undef

by swl (Parson)
on Feb 22, 2021 at 20:55 UTC ( [id://11128667]=note: print w/replies, xml ) Need Help??


in reply to Surprising result when using undef

undef is not the same as zero.

I see a couple of options you could use.

use 5.010; my $is_dev_mode = $param->{is_dev_mode} // 1;

Or for perls older than 5.10:

my $is_dev_mode = defined $param->{is_dev_mode} ? $param->{is_dev_mode +} : 1;

And if you want to get a bit more obtuse and do all the value handling on one line then you can use the double negative ("bang bang" operator). That will convert any non-zero (or true) value to a 1, and any false value to perl's false. Then that can be converted to a zero.

# it would likely help your future self to add a comment here if you +do this my $is_dev_mode = !!$param->{is_dev_mode} || 0;

Another alternative is to use a boolean flag to test for non-dev mode, instead of dev mode. Then you only need to check for truthiness of the passed flag to determine whether to send the emails or not. That avoids all the value checking and changing, simplifying your code. It also means that if the flag is not passed with a true value then the emails will not be sent.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-19 07:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found