Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by Anonymous Monk
on Nov 10, 2003 at 06:54 UTC ( #305805=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Re: Re^4: m//g behaves strange... (!1)
Select or Download Code
Re: Re: Re: Re^4: m//g behaves strange... (!1)
by ysth (Canon) on Nov 10, 2003 at 07:14 UTC
    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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (13)
As of 2014-08-22 18:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (163 votes), past polls