Does perl have an internal function to unquote a variable? -
it not uncommon parameter passed few times.
here interesting example of losing quote can helps.
the first abs() return variable without quote, making second abs return correct value.
my question is:
does perl have internal function unquote variable not have code way?
#!/usr/bin/perl -w use strict; @nums = ( '-0', '-0.0', "-0.000", qw(-0.000), sprintf("%.4f", "-0.0"), ); print "***use single abs()\n"; foreach $num(@nums){ $number = $num; $abs = abs($number); print "<$num> abs <$abs>\n"; } print "\n***use abs(abs())\n"; foreach $num(@nums){ $abs_abs = abs(abs($num)); print "<$num> double abs <$abs_abs>\n"; } system information:
uname -r 2.6.32-573.12.1.el6.centos.plus.x86_64 perl, v5.10.1 (*) built x86_64-linux-thread-multi the screen output:
***use single abs() <-0> abs <0> <-0.0> abs <-0> <-0.000> abs <-0> <-0.000> abs <-0> <-0.0000> abs <-0> ***use abs(abs()) <-0> double abs <0> <-0.0> double abs <0> <-0.000> double abs <0> <-0.000> double abs <0> <-0.0000> double abs <0>
that's not issue quotes -- quotes perl syntax tell interpreter string begins , ends. perl knows value being stored string, quotes not stored in memory.
rather, it's artifact of special floating point value "-0.0". use, equivalent value 0.0
perl -e ' $p = 0.0; $n = -0.0; $p == $n ' ==> 1 perl -e ' $p = 0.0; $n = -0.0; $x = 4.2; $p+$x == $n+$x ' ==> 1 the 2 exceptions, far can tell, binary representation , string representation.
$ perl -e 'print pack "f",0.0' | od -c 0000000 \0 \0 \0 \0 \0 \0 \0 \0 0000010 $ perl -e 'print pack "f",-0.0' | od -c 0000000 \0 \0 \0 \0 \0 \0 \0 200 0000010 $ perl -mdevel::peek -e 'dump($n=-0.0),dump($p=0.0)' sv = nv(0x43523d8) @ 0x434ff00 refcnt = 1 flags = (nok,pnok) nv = -0 sv = nv(0x43523e0) @ 0x4329b58 refcnt = 1 flags = (nok,pnok) nv = 0 $ perl -e '$p=0.0; $n=-0.0; $p,$n' 0 -0 (actually, perl v5.12 see 0,-0 v5.16 0,0 -- maybe noticed , fixed it)
the integers 0 , -0 not have issue. abs(-0.0) returns integer 0, , accident abs appears resolve "quoting" issue.
Comments
Post a Comment