Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Inchworm vs scalar: on boolean false value

by Eily (Monsignor)
on Dec 17, 2019 at 14:20 UTC ( [id://11110277]=note: print w/replies, xml ) Need Help??


in reply to Inchworm vs scalar: on boolean false value

There are two things at work here. First, the false value of perl is actually neither strictly '' nor 0, it's kinda both (link to the doc when I find it). As demonstrated by

perl -E "use strict; use warnings; say( '' + 1 ); say( !1 + 1 ); say ! +1; say '!1 is defined' if defined !1" Argument "" isn't numeric in addition (+) at -e line 1. 1 1 !1 is defined
The warning about the non numeric argument happens only once, because !1 is treated as a number in arithmetic context. But printing !1 doesn't show anything because false is the empty string in string context.

~ however only works in two ways: bitwise not on a number, or bitwise not on a binary string. And the output will be of the same type. This means that ~~ is only equivalent to scalar when the operand is a number or a string, but it will force another type (probably string) on any other value (an object, a reference...).

Edit: one place where the special value of perl's false is mentioned: Relational Operators.
Also, ~~ is not equivalent to scalar for some big numbers and negative numbers:

DB<1> say ~~ (-1) 18446744073709551615
So it's better not to use it outside of obfuscated code.

Replies are listed 'Best First'.
Re^2: Inchworm vs scalar: on boolean false value
by haukex (Archbishop) on Dec 17, 2019 at 21:55 UTC
    the false value of perl is actually neither strictly '' nor 0, it's kinda both (link to the doc when I find it).

    Truth and Falsehood :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-18 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found