I made several changes to your program:
#! /usr/bin/env perl
My Mac runs 10.8.6, and I use Perlbrew to test 5.8.9, 5.10, 5.12, and 5.18. From the first two I get the following:
Scalar version: 1.19 "123" doesnt look like a number "123" looks like a number "123" looks like a number
With two other versions, I get the following:
Scalar version: 1.22 "123" looks like a number "123" looks like a number "123" looks like a number
Then I made another small change to your program. Instead of just using $1 I set my $test = $1 and then ran looks_like_number( $test ) ;
After that I got the following:
Scalar version: 1.19 "123" looks like a number "123" looks like a number "123" looks like a number
One of the things you should understand is that $1 , $_ and their ilk are not only global variables, but always in the main namespace. This means that if you call a subroutine with $1 , it is possible that the subroutine may end up changing the value.
You should always assign your own variables (and preferably my variables) instead of relying on Perl special variables. This means that you are not using $_ in the while and for loops, but not processing @_ directly in the routines. And when you use regex, you should assign your own variables $1 , $2 , etc., as soon as you can.
By the way, the most interesting difference between Scalar :: Util versions 1.22 and 1.19 is that version 1.22 will use the compiled C code, if it exists. If not, it loads Scalar :: Util :: PP, which is a version of Perl. However, when I used Scalar::Util::PP in Perl 5.12 and 5.18, I still get the correct output.
Curiosity and curiosity
I tried modifying Scalar::Util to find out what was going on. However, I see:
require List::Util;
And the version of Perl code is enclosed in a large Eval statement. Interestingly, the side effect only gets into the first, although the routine sets the line and starts the regular expression. The same code, but performs two different methods.
Adding quotes around $1 makes it behave correctly:
if ( looks_like_number "$1" ) {
Also just printing it does the job:
print "The value is " . $1 . "\n"; if ( looks_like_number $1 ) {
Or assign it to another variable, but not using this variable:
my $test = $1; if ( looks_like_number $1 ) { works now...
Let us use the debugger and see if we can trace the problem:
$ perl -d test.pl Loading DB routines from perl5db.pl version 1.31 Editor support available. Enter h or `hh' for help, or `man perldebug' for more help. main::(test.pl:7): print "Version of Scalar::Util: $Scalar::Util::VERSION\n"; DB<1> c Version of Scalar::Util: 1.19 "123" looks like a number. "123" looks like a number. "123" looks like a number. Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, hq, h R or ho to get additional info.
It works? Try the same command without the -d :
$ perl test.pl Version of Scalar::Util: 1.19 "123" doesn't look like a number. "123" looks like a number. "123" looks like a number.
How can you trace this problem if you cannot see it in the debugger?