Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Tk Entry widget confusion

by hv (Prior)
on Jul 12, 2024 at 11:19 UTC ( [id://11160563]=note: print w/replies, xml ) Need Help??


in reply to Tk Entry widget confusion

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.

Replies are listed 'Best First'.
Re^2: Tk Entry widget confusion
by colintu (Acolyte) on Jul 12, 2024 at 11:59 UTC
    Thanks, that does explain what I'm seeing.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-09-20 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (25 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.