Reputation: 63
I'm trying to figure out how to use perl's eval to get a single element array. So far, I have:
use strict;
use warnings;
use Data::Dumper;
sub printit
{
my $param = shift;
my $thing = eval $param;
if (ref($thing) =~ /HASH/ || ref($thing) =~ /ARRAY/) {
print Dumper \$thing;
} else {
print $param . "\n";
}
}
my $ip = "192.168.1.100";
my $ip_array = "[192.168.1.100]";
my $ip_array2 = "[192.168.1.100,]";
my $string = "{ a => 1, b => 2, c => 3}";
my $another_string = "[1, 2, 3 ]";
printit($ip);
printit($string);
printit($another_string);
printit($ip_array);
printit($ip_array2);
My output looks like:
[user]$ perl ~/tmp/cast.pl
192.168.1.100
$VAR1 = \{
'c' => 3,
'a' => 1,
'b' => 2
};
$VAR1 = \[
1,
2,
3
];
$VAR1 = \[
"\x{c0}\x{a8}d"
];
$VAR1 = \[
"\x{c0}\x{a8}d"
];
I think I'm getting a scalar ref for the last 2 print outs but I want an array with a single element like this:
$VAR1 = \[
"192.168.1.100"
];
Upvotes: 2
Views: 333
Reputation: 385655
You say you want
[ "192.168.1.100" ]
but the code you pass to Perl is
[ 192.168.1.100 ]
Those are very different.
"192.168.1.100"
creates the 13-character string 192.168.1.100
.
192.168.1.100
is short for v192.168.1.100
, and it creates the same 4-character string as chr(192).chr(168).chr(1).chr(100)
.
One of many ways of writing what you want:
my $ip_array = '["192.168.1.100"]';
Upvotes: 1
Reputation: 118595
eval "[192.168.1.100]"
is an array reference, not a scalar reference. The array reference contains one element, but it is not the string "192.168.1.100"
as you might expect, because 192.168.1.100
is not quoted. Instead you are creating the version string 192.168.1.100
.
The fix is to include a quote or quoting operator in your input.
my $ip_array = "['192.168.1.100']";
my $ip_array2 = "[qq/192.168.1.100/,]";
See "Version Strings" in perldata.
Upvotes: 5