Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^2: how ! operator works

by moritz (Cardinal)
on Feb 04, 2011 at 08:51 UTC ( #886157=note: print w/replies, xml ) Need Help??

in reply to Re: how ! operator works
in thread how ! operator works

This "False" value is basically the same as

use Scalar::Util qw/dualvar/; dualvar(0, '');

Which stems from the curious fact that a Perl scalar internally has a slot for both a numeric and a string value. By explicitly providing a numeric value, you don't get a warning when used as a number:

$ perl -wE 'say 1 + ""' Argument "" isn't numeric in addition (+) at -e line 1. 1 $ perl -MScalar::Util=dualvar -wE 'say dualvar(0, "") + 1' 1

Note that when abused, these dualvars can be very confusing:

$ perl -MScalar::Util=dualvar -E 'my $x = dualvar(42, "23"); say $x; s +ay $x + 1' 23 43

Devel::Peek shows that the False value is a wee bit more special than that:

perl -MDevel::Peek -MScalar::Util=dualvar -e 'Dump !1; Dump dualvar(0, + "")' SV = PVNV(0x254ff70) at 0x7409c0 REFCNT = 2147483647 FLAGS = (IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK) IV = 0 NV = 0 PV = 0x254ff50 ""\0 CUR = 0 LEN = 8 SV = PVNV(0x2550d90) at 0x2551220 REFCNT = 1 FLAGS = (TEMP,IOK,POK,pIOK,pPOK) IV = 0 NV = 0 PV = 0x255f610 ""\0 CUR = 0 LEN = 8

So it's not only a dualvar, but it's also readonly, and has a very high refcount - it's basically a singleton.

Replies are listed 'Best First'.
Re^3: how ! operator works
by arkturuz (Curate) on Feb 04, 2011 at 09:12 UTC
    That refcount (2147483647) looks strangely familiar. Is that a coincidence or just a hack in the interpreter so it (the object in question) never gets destroyed by the garbage collector (theoretically)?
      That refcount (2147483647) looks strangely familiar.
      Just print it out in hex and you'll see:
      which is a clear indication that this is the largest positive integer you can stuff into a signed 32 bit integer (2's complement).

      So yeah, your guess is very likely correct.

      So I looked at the code to answer your question. It looks like it's a hack, not a magical value. The refcount can go down, but it gets reset back to the max for SvIMMORTAL variables (the true, the false, the undef and a special internal thing called "placeholder").

        So I looked at the code to answer your question.

        Thanks! I searched the code myself today with no luck. I postoponed it for the weekend but now you have resolved this, I'll just have to stick to something else to do :)
Re^3: how ! operator works
by !1 (Hermit) on Feb 04, 2011 at 18:53 UTC
    ...what'd you just call me?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://886157]
[ambrus]: ambrus's scratchpad
[ambrus]: Also, I was wrong, apparently you have to support both periodic and one-shot timers in an AnyEvent backend
[Corion]: ambrus: That looks deceptively simple, but maybe that's simply all that's needed. Which would be great! I hope I get to test it tomorrow, thanks!!
[choroba]: Hm, unrelated question.
[ambrus]: I added a timer implementation to ambrus's scratchpad as well, again the whole thing is completely untested and probably has more bugs than lines
[choroba]: Anyone using anything to validate json contents of a REST service?

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2016-12-08 12:46 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (141 votes). Check out past polls.