Perl - Is this a bug with looks_like_number, or am I being a stupid person? -
this code:
#!/usr/bin/perl -w use strict; use scalar::util qw(looks_like_number); sub what_the_fudge { $string = "foo 123 bar"; if ($string =~ /foo (.+) bar/) { if (looks_like_number($1)) { print "$1 looks number\n"; } else { print "$1 doesnt number\n"; } } } &what_the_fudge; &what_the_fudge; &what_the_fudge;
displays this:
123 doesnt number 123 looks number 123 looks number
why fail recognize number first time? =( baffles me.
some information environment:
os: osx 10.6.8
perl -e 'use scalar::util; print "$scalar::util::version\n"'
--> 1.19
perl -v
--> perl, v5.10.0 built darwin-thread-multi-2level (with 2 registered patches, see perl -v more detail)
it's bug fixed in scalar::util 1.20. ("handle overloaded , tied values" in changes
file.)
the xs version of looks_like_number
failed handle magical arguments properly. magic allows code called when operations performed on variable (such fetching value).
solutions:
- upgrade scalar::util 1.20 or higher.
- delete compiled component of scalar::util force use of pure perl implementation doesn't suffer problem.
- use
looks_like_number("$1")
creates non-magical copy of$1
right value.
1.19:
int looks_like_number(sv) sv *sv prototype: $ code: #if (perl_version < 8) || (perl_version == 8 && perl_subversion <5) if (svpok(sv) || svpokp(sv)) { retval = looks_like_number(sv); } else { retval = svflags(sv) & (svf_nok|svp_nok|svf_iok|svp_iok); } #else retval = looks_like_number(sv); #endif output: retval
1.21:
int looks_like_number(sv) sv *sv prototype: $ code: sv *tempsv; if (svamagic(sv) && (tempsv = amg_callun(sv, numer))) { sv = tempsv; } else if (svmagical(sv)) { svgetmagic(sv); } #if (perl_version < 8) || (perl_version == 8 && perl_subversion <5) if (svpok(sv) || svpokp(sv)) { retval = looks_like_number(sv); } else { retval = svflags(sv) & (svf_nok|svp_nok|svf_iok|svp_iok); } #else retval = looks_like_number(sv); #endif output: retval
try this:
what_the_fudge("foo 123 bar"); what_the_fudge("foo baz bar"); what_the_fudge("foo 123 bar"); sub what_the_fudge { $string = shift;
i haven't tried it, should get
version of scalar::util: 1.19 doesnt number looks number doesnt number
Comments
Post a Comment