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


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

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 FLAGS = (PADBUSY,PADMY,NOK,POK,pNOK,pPOK) IV = 0 NV = 0 PV = 0x8124658 ""\0 CUR = 0 LEN = 1 SV = PVNV(0x8143058) at 0x8129384 REFCNT = 1 FLAGS = (PADBUSY,PADMY,NOK,POK,pNOK,pPOK) IV = 0 NV = 0 PV = 0x8126180 ""\0 CUR = 0 LEN = 1 SV = PV(0x811fab8) at 0x81293c0 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x8149ce0 ""\0 CUR = 0 LEN = 1

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