Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

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

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):

  • The string is a stringification of the IV.
  • The string is a stringification of the NV.
  • The number is a numification of the string.

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
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 sharing their wisdom with the Monastery: (3)
As of 2024-04-19 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found