But why doesn't this confuse Perl?

Using the output of your first snippet as an example,

SV = PVIV(0x8150b10) at 0x814f6b4 <-- Can contain an IV and PV REFCNT = 1 FLAGS = (IOK,pIOK) <-- Only contains an IV IV = 123 PV = 0x81623f0 "abc"\0 CUR = 3 LEN = 4 SV = PVIV(0x8150b10) at 0x814f6b4 <-- Can contain an IV and PV REFCNT = 1 FLAGS = (POK,pPOK) <-- Only contains a PV IV = 123 PV = 0x81623f0 "123"\0 CUR = 3 LEN = 4

The type of the scalar is promoted as necessary. Downgrading would be a waste of time, so flags are used to indicate what kind of value the scalar contains.

Note that it's possible to have a scalar that has both an IV and PV.

$ perl -MDevel::Peek -e'$x = 123; Dump $x; "$x"; Dump $x' SV = IV(0x816a41c) at 0x814f69c REFCNT = 1 FLAGS = (IOK,pIOK) <-- Only contains an IV IV = 123 SV = PVIV(0x8150b10) at 0x814f69c REFCNT = 1 FLAGS = (IOK,POK,pIOK,pPOK) <-- Stringification has IV = 123 been cached. PV = 0x81651a0 "123"\0 CUR = 3 LEN = 4

On the other hand, Storable seems to blindly favor PV when both IV and PV exist,

When both IV and PV exist (i.e. when the scalar is a PVIV or subtype), or when POK is set? The former would definitely be a bug. Otherwise, read on.

If a scalar contains both a number and a string (POK and either IOK or NOK), the situation is intended to be one of the following (and almost always is):

If we accept that more general dualvars can't be stored, we should be able to store only one of the string or the number. So which one should we used when?

The string is a stringification of the IV.

Stringification of IVs is lossless, and so is their numification.

Either the string or the IV will do.
The string is a stringification of the NV. Stringification of NVs is practically lossless, and so is their numification. Either the string or the NV will do.

The number is a numification of the string.

Numification of non-numeric strings is lossy. e.g 0+"abc" ne "abc".

Numification of partially numeric strings is lossy. e.g 0+"123abc" ne "123abc".

Numification of some numeric strings is lossy. e.g 0+"0.50" ne "0.50".

Necessarily, we need to store the string.

As such, always storing the string is sufficient and appropriate.

Update: Storing the string instead of the number also allows us to store '0 but true' and '0E0', true values that evaluate to zero.


In reply to Re^3: Why do $x="$h->{foo}" and $x=$h->{foo} have different effects (as reported by Devel::Peek)? by ikegami
in thread Why do $x="$h->{foo}" and $x=$h->{foo} have different effects (as reported by Devel::Peek)? by ELISHEVA

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.