In the code below, we see that Dumper can tell whether a string variable (a variable for which the ref value is an empty string) is currently stored as numeric or as string. Is there any way, simpler than by using Dumper, to answer this question about a variable? Am I correct that a string variable contains perl magic which answers this question, if one could access this magic more directly?
#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper qw(Dumper);
my $var;
$var=55;
print ref $var; print Dumper $var;
$var='55';
print ref $var; print Dumper $var;
output:
$VAR1 = 55;
$VAR1 = '55';
Some of the reasons I want to understand this:
I build
HASH
es and/orARRAY
s where the hash values or the array elements are four-digit years. The numbers come from various sources, sometimes from regex substitutions, sometimes from the command line, so they show up both as numeric and as string. But I want them all to line up exactly when I display them using Dumper.I am curious, which is ultimately why I write code in the first place.
Somebody said that
==
is a more efficient check thaneq
, and I want to know exactly what kind of check I am implementing.There is a nifty way to force a string which looks like a number, to be stored as a number, as we see in How can I convert a string to a number in Perl?. See @Håkon Hægland 's comment. But I want to check or peek at a variable without changing it at all. Because I want to understand as clearly as possible what my code is doing, before it throws a warning or an error.
As @Håkon Hægland pointed out, there is a
meta::cpan
module which may work, if installed. But it does not come standard with my perl:This is perl 5, version 18, subversion 4 (v5.18.4) built for darwin-thread-multi-2level
and I want to write code that does not need enhancement at the system level. I want my scripts to run on any new macbook pro or linux machine that I eventuallyrsync
my files to. My attempt touse Scalar::Type qw(is_*);
throwsCan't locate Scalar/Type.pm in @INC (you may need to install the Scalar::Type module)
. Is it really not possible to answer this question using "out of the box" perl?