http://www.perlmonks.org?node_id=305802

in reply to Re^4: m//g behaves strange... (!1)

Yes, I know what you said. If this is the logical answer, so simple and straight, I have used it in my previous post already. Does not seem to me that you have read and understood my previous post carefully. What you said is right, but it does not mean anything here.

Also I don't understand why you involve !1, when you can simply state that both 0 and "" are "false". How is this "!" operator get involved, when we are talking about "~" operator. They are relatives, but not the same person.

Although Perl consider multiple values as "false", for a particular function, it usually always returns a single fixed consistent value to represent "false". Ask you an extream question, both 1 and 2 represents "true", when one of your function needs to return "true", will you consistently return 1, or will you sometime return 1 and sometime return 2?

You failed to answer why should "~" receive 0, when "~" can be applied against "empty string" with no problem, and as we see thru lots of my demo, m// actually returns "empty string" from time to time to represent "false".

Your 0+"" example also means nothing here. In your case, "+" operator has to be applied to numbers, but in my case, it is absolutely valid to use "~" operator against a string. try this:

```use strict;
use warnings;
print ~"abcd";

From a logic point of view, what you did is like: I asked how much 1+1 is, and you answered 2+3=5.

Update:

Anonymous Monk's dump is useful, and my further explanation is that:

• as print requires a string value, so "" is used.
• for "~", although it can be used against both string and number, when both "" and 0 are available to be used, Perl arbitrarily picks 0.

One reminder, the dump you given shows ""\0, please don't make up this !1. State what you observed, and only what you observed.

Replies are listed 'Best First'.
Re: Re: Re^4: m//g behaves strange... (!1)
by Anonymous Monk on Nov 10, 2003 at 06:54 UTC

tye is correct, as usual. The return value of m// when false in scalar context is !1, which is special in that both numeric and string slots are flagged on, with the string value "" and the numeric value 0. The unary ~ op will use the integer value if that flag is on, so it really sees a 0 (not a conversion of the empty string to a numeric value).

```#!/usr/bin/perl -w
use strict;
use Devel::Peek;
my \$x = !1;
Dump \$x;

\$_ = 'foo';
my \$y = /x/;
Dump \$y;

my \$z = "";
Dump \$z;
__END__
SV = PVNV(0x8143040) at 0x81293cc
REFCNT = 1
IV = 0
NV = 0
PV = 0x8124658 ""\0
CUR = 0
LEN = 1
SV = PVNV(0x8143058) at 0x8129384
REFCNT = 1
IV = 0
NV = 0
PV = 0x8126180 ""\0
CUR = 0
LEN = 1
SV = PV(0x811fab8) at 0x81293c0
REFCNT = 1
PV = 0x8149ce0 ""\0
CUR = 0
LEN = 1

So we were both incorrect: the return value is neither "" nor 0 but !1.

You beat me out - I had an almost identical reply ready when I saw yours. Let me just add that ~ tries to use an integer value even if only a floating-point value is set, and even if it is out of integer range (which can result in some strange results.)

Also, for any of the bitwise/stringwise ops (~, &, ^, |), quote the variable to force stringwise semanics if you are unsure where it has been:

```\$ perl -wle'\$x="abc"; print ~\$x; print \$x+0; print ~\$x; print ~"\$x"'
z?o
Argument "abc" isn't numeric in addition (+) at -e line 1.
0
18446744073709551615
z?o
Re^6: m//g behaves strange... (!1!)
by tye (Sage) on Nov 10, 2003 at 15:34 UTC
Does not seem to me that you have read and understood my previous post carefully. What you said is right, but it does not mean anything here.

I was only adding a bit of information on the subject. That is quite a rant you responded with. Perhaps you should take a step back.

To be very clear, let's start back at the beginning. You said:

in scalar context, m// returns either 1 or 0.
to which AnonyMonk replied:
<pedantic> actually, m// in scalar context returns either 1 or "" (empty string). </pedantic>.
And to be extra clear, I'll be blunt: Y'all are both wrong (and y'all are both right). When m// fails in a scalar context, it returns 'the false value'. That is, it returns the standard value that the perl interpreter itself uses all over the place when 'false (but defined)' is to be returned. It is prepopulated with both a string value ("") and a numeric value (0 -- actually both the IV 0 and the NV 0.0 according to the dumps provided by AnonyMonk).

In C code, this value has a short name, PL_sv_no. When talking Perl, it also deserves a short name. A very short and useful name for it is !1 (useful because you can actually use it in your Perl code when you want a really good 'false' value).

You failed to answer why should "~" receive 0, when "~" can be applied against "empty string" with no problem, and as we see thru lots of my demo, m// actually returns "empty string" from time to time to represent "false".

Yes, I didn't say anything about that. I guessed that you already knew why.