I always thought an assignment was an assignment, but I guess I'm wrong. I wonder if anyone can explain to me why the following two sets of assignments generate different results for PV (when dumped via Devel::Peek)? This issue came up because, apparently Storable uses the PV value, not the IV value when freezing.while running some regression tests, I discovered that Storable was generating two different frozen strings for what appeared to be the same variable with the same assigned value. (See update below for more information)

$x = $hIn->{string}; $x = $hIn->{number}; Devel::Peek::Dump($x); #and $x = $hIn->{string}; $x = "$hIn->{number}"; Devel::Peek::Dump($x);

I might have thought that the datum assigned via direct assignment and interpolation might be different. But if that was so, why then do the following two bits of code also produce different results? We haven't (in Perl) actually assigned anything and yet the mere fact of using the variable in an interpolation seems to change it:

$x = $hIn->{string}; $x = $hIn->{number}; Devel::Peek::Dump($x); #followed immediately by print "$x\n"; Devel::Peek::Dump($x);

I've written a sample script to illustrate the differences and provided sample output from my machine (running Debian-Etch, Perl 5.8.8):

use strict; use warnings; use Devel::Peek; my $hIn = {string => 'abc', number => 5 }; print "#*** Setting...: " . q{$x=$hIn->{string}} . "\n"; my $x = $hIn->{string}; # we assigned a new value to $x BUT # Devel::Peek::Dump claims that $x has PV = "abc"\0 # !!!! ???? print STDERR "#*** " . q{$x = $hIn->{number};} . "\n"; $x = $hIn->{number}; Devel::Peek::Dump($x); # but after being used in an interpolation # Devel::Peek::Dump says that $x has PV = "5"\0 print STDERR "#*** " . q{print "<$x>\n"} . "\n"; print "<$x>\n"; Devel::Peek::Dump($x); print "#*** Resetting...: " . q{$x=$hIn->{string}} . "\n"; $x = $hIn->{string}; # we assigned a new value to $x BUT # Devel::Peek::Dump claims that $x has PV = "abc"\0 # !!!! ???? print STDERR "#*** " . q{$x = $hIn->{number};} . "\n"; $x = $hIn->{number}; Devel::Peek::Dump($x); # but after being used in an interpolation # Devel::Peek::Dump says that $x has PV = "5"\0 print STDERR "#*** " . q{"$x = $hIn->{number};"} . "\n"; $x = "$hIn->{number}"; Devel::Peek::Dump($x);

outputs the following:

#*** Setting...: $x=$hIn->{string} #*** $x = $hIn->{number}; SV = PVIV(0x8150b10) at 0x814f624 REFCNT = 1 FLAGS = (PADBUSY,PADMY,IOK,pIOK) IV = 5 PV = 0x81902e8 "abc"\0 CUR = 3 LEN = 4 #*** print "<$x>\n" <5> SV = PVIV(0x8150b10) at 0x814f624 REFCNT = 1 FLAGS = (PADBUSY,PADMY,IOK,POK,pIOK,pPOK) IV = 5 PV = 0x81902e8 "5"\0 CUR = 1 LEN = 4 #*** Resetting...: $x=$hIn->{string} #*** $x = $hIn->{number}; SV = PVIV(0x8150b10) at 0x814f624 REFCNT = 1 FLAGS = (PADBUSY,PADMY,IOK,pIOK) IV = 5 PV = 0x81902e8 "abc"\0 CUR = 3 LEN = 4 #*** "$x = $hIn->{number};" SV = PVIV(0x8150b10) at 0x814f624 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) IV = 5 PV = 0x81902e8 "5"\0 CUR = 1 LEN = 4

Many thanks in advance, beth

Update: added explanation of why this became an issue for me.

Update: corrected intro paragraph (with strikeout). With the help of kyle and ikegami I now have a much better understanding of the Devel::Peek output. It appears that my first impression (that Storable was ignoring flags and preferring PV was wrong. Storable does indeed choose between IV and PV correctly. The differences I was seeing was because it also seems to be storing the flags along with the value. So if something changes the flags the output of Storable::freeze(...) changes as well, even if the actual value stored does not. This doesn't appear to be a bug, but it is something to be aware of if one ever needs to work with frozen strings.

Once again, many thanks for the wonderful explanations all have provided. I have learned a great deal from this thread.

Best, beth


In reply to 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.