ELISHEVA has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Why do $x="$h->{foo}" and $x=$h->{foo} have different effects (as reported by Devel::Peek)?
by borisz (Canon) on Mar 25, 2009 at 16:52 UTC | |
by ELISHEVA (Prior) on Mar 25, 2009 at 17:19 UTC | |
by ikegami (Patriarch) on Mar 25, 2009 at 17:46 UTC | |
|
Re: Why do $x="$h->{foo}" and $x=$h->{foo} have different effects (as reported by Devel::Peek)?
by derby (Abbot) on Mar 25, 2009 at 17:06 UTC | |
|
Re: Why do $x="$h->{foo}" and $x=$h->{foo} have different effects (as reported by Devel::Peek)?
by kyle (Abbot) on Mar 25, 2009 at 17:15 UTC | |
|
Re: Why do $x="$h->{foo}" and $x=$h->{foo} have different effects (as reported by Devel::Peek)?
by Anonymous Monk on Mar 26, 2009 at 02:52 UTC |