Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Question on SV internals

by ikegami (Patriarch)
on May 08, 2011 at 15:54 UTC ( [id://903648]=note: print w/replies, xml ) Need Help??


in reply to Re: Question on SV internals
in thread Question on SV internals

Yes, until you next use it in a string context.

There's no such terminating condition.

>perl -MDevel::Peek -e"$_ = '20 ducks'; Dump($_); 0+$_; Dump($_); ''.$ +_; Dump($_);" ... SV = PVNV(0x163a454) at 0x342be4 REFCNT = 1 FLAGS = (POK,pIOK,pNOK,pPOK) IV = 20 NV = 20 PV = 0x1639b14 "20 ducks"\0 CUR = 8 LEN = 12

Another way to check is to count the number of numeric warnings:

>perl -wE"$_='20 ducks'; say 0+$_; say 0+$_; say ''.$_; say 0+$_;" Argument "20 ducks" isn't numeric in addition (+) at -e line 1. 20 20 20 ducks 20

Replies are listed 'Best First'.
Re^3: Question on SV internals
by BrowserUk (Patriarch) on May 08, 2011 at 16:42 UTC

    $s = '20 ducks';; Dump $s;; SV = PV(0x18aec0) at 0xa5b48 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x40cad18 "20 ducks"\0 CUR = 8 LEN = 16

    Length = 16. Now use it in a numeric constant.

    $s += 1;; Argument "20 ducks" isn't numeric in addition (+) at (eval 13) line 1, + <STDIN> line 4. Dump $s;; SV = PVNV(0x40b1eb8) at 0xa5b48 REFCNT = 1 FLAGS = (NOK,pNOK) IV = 20 NV = 21 PV = 0x40cad18 "20 ducks"\0 CUR = 8 LEN = 16

    Hm. Length still 16, but we gained an IV = 20 and an NV = 21; so we gained 12 or 16 bytes.

    Oh, and the PV no longer reflects the value of the scalar, so when you use it in a string context, a conversion has to happen!

    print $s;; 21 Dump $s;; SV = PVNV(0x40b1eb8) at 0xa5b48 REFCNT = 1 FLAGS = (NOK,POK,pNOK,pPOK) IV = 20 NV = 21 PV = 0x40d3068 "21"\0 CUR = 2 LEN = 40

    Which modifies the PV and oops, it grew to 40 bytes in the process.

    And I don't care how you try to weasel out of it, the evidence is right there for those that care to look. So don't bother.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Now use it in a numeric constant. $s += 1;

      $s += 1 does more than "use it in a numeric context".

      Update:

      Furthermore, even though you went outside the scope of the question, the scalar still has a numerical value throughout.

        FLAGS = (NOK,pNOK)
        FLAGS = (NOK,POK,pNOK,pPOK)

      There is no such terminating condition.

        does more than "use it in a numeric context".

        And of course, that could never happen in a real program. Take your blinkers off.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found