J
jl_post
Hi,
I'm wondering if there's a way in Perl to tell if a scalar was set
as a string. For example, I'd like to write code like this:
my $var1 = "7777";
my $var2 = 7777;
foreach ($var1, $var2)
{
if ( SOME_TEST )
{
# Value is a string, so print it with quotes:
print "Value = \"$_\"\n";
}
else
{
# Print it without quotes:
print "Value = $_\n";
}
}
The reason I want to do this is because I write a lot of Perl code
that uses the unpack() function, and then reports the data it
extracts. For example, I might write:
$string = 'A 65';
@data = unpack('c x a2', $string);
in which case @data should be filled with two elements: the first
being the integer 65, and the second the string "65". But since Perl
seamlessly converts strings to the numeric values they contain, it can
be hard for me to know whether a value that looks numeric was
originally a string or a number.
I noticed that if I use the perl debugger (with "perl -wde 1"),
typing "x @data" does not show which element is a string. On the
other hand, "use Data:umper; print Dumper @data;" DOES show the
difference -- and it shows it by surrounding the second element with
single quotes. (Evidently Perl does keep track of whether a numeric
scalar was set as a string or not.)
So my question is: How can I know if a scalar (that looks like a
number) was originally a string? (That is, assigned/encoded to a
string?)
I've read "perldoc Scalar::Util", but the only functions that look
related are looks_like_number() (which is not what I want because
regardless of whether it looks like a number I want to know if it was
encoded as a string) and isvstring() (which returns true if the value
was coded as a "vstring" (but not as a string, which is what I want)).
Thanks in advance for any help.
-- Jean-Luc
I'm wondering if there's a way in Perl to tell if a scalar was set
as a string. For example, I'd like to write code like this:
my $var1 = "7777";
my $var2 = 7777;
foreach ($var1, $var2)
{
if ( SOME_TEST )
{
# Value is a string, so print it with quotes:
print "Value = \"$_\"\n";
}
else
{
# Print it without quotes:
print "Value = $_\n";
}
}
The reason I want to do this is because I write a lot of Perl code
that uses the unpack() function, and then reports the data it
extracts. For example, I might write:
$string = 'A 65';
@data = unpack('c x a2', $string);
in which case @data should be filled with two elements: the first
being the integer 65, and the second the string "65". But since Perl
seamlessly converts strings to the numeric values they contain, it can
be hard for me to know whether a value that looks numeric was
originally a string or a number.
I noticed that if I use the perl debugger (with "perl -wde 1"),
typing "x @data" does not show which element is a string. On the
other hand, "use Data:umper; print Dumper @data;" DOES show the
difference -- and it shows it by surrounding the second element with
single quotes. (Evidently Perl does keep track of whether a numeric
scalar was set as a string or not.)
So my question is: How can I know if a scalar (that looks like a
number) was originally a string? (That is, assigned/encoded to a
string?)
I've read "perldoc Scalar::Util", but the only functions that look
related are looks_like_number() (which is not what I want because
regardless of whether it looks like a number I want to know if it was
encoded as a string) and isvstring() (which returns true if the value
was coded as a "vstring" (but not as a string, which is what I want)).
Thanks in advance for any help.
-- Jean-Luc