Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Surprising result when using undef

by swl (Priest)
on Feb 22, 2021 at 20:55 UTC ( #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
Node Status?
node history
Node Type: note [id://11128667]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2021-04-11 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?