Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

This is probably the primary magic of Perl: values are silently converted to the type required for the operator acting on them as far as possible. Behind the curtain, it caches those conversions and remembers whether they are exact or lossy.

This also allows for fun tricks, such as "dualvars" that have a different cached value in the string slot and the integer slot - that's used, for example, to allow $! to reveal both the error message and the error number:

% perl -wE 'stat("nonexistent"); $e = $!; say $e; say $e+0' No such file or directory 2

You can see what's going on in gory detail with a module such as Devel::Peek:

% perl -MDevel::Peek -we ' $val = ""; Dump($val); $val ||= 0; Dump($val); $val =~ /^\d+$/; Dump($val); ' 2>&1 | grep V SV = PV(0x561cd5e52da0) at 0x561cd5e7a028 PV = 0x561cd5e9b150 ""\0 SV = PVIV(0x561cd5e74e60) at 0x561cd5e7a028 IV = 0 PV = 0 SV = PVIV(0x561cd5e74e60) at 0x561cd5e7a028 IV = 0 PV = 0x561cd5e7b6a0 "0"\0

$val is a scalar value ("SV") as opposed to an array value ("AV"), hash value ("HV"), or something more exotic. The SV has slots for a string ("pointer value" = "PV"), an integer ("IV") and a (floating-point) numeric value ("NV").

In the example above, we start off with a plain string (PV = ""). Then we replace it with an integer 0 (IV = 0, PV is cleared out); then we use that in a string context so it generates and caches the stringified version (PV = "0").

The hex values in there are memory addresses. You don't normally care about them, but it can be useful to see whether two memory addresses are the same or not.

I've grepped out most of the gore here; that includes the reference count (which Perl uses to know when a value should be freed); the length of the string value (CUR) and how much memory is allocated for it (LEN, confusingly); flags that say which of the value slots are valid (IOK, POK, NOK) and partially valid (pIOK, pPOK, pNOK); and a flag (IsCOW) and associated reference count if the string is sharable, as a memory saving.

Almost all of the time you don't need to know any of this, but having at least some clue what's going on behind the curtain can be very helpful to make Perl seem more predictable.


In reply to Re: Tk Entry widget confusion by hv
in thread Tk Entry widget confusion by colintu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    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-09-16 23:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      The PerlMonks site front end has:





      Results (22 votes). Check out past polls.

      Notices?
      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.