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"
];
my $ip_array = "['192.168.1.100']";show the same problems?print Dumper \$thing;should beprint Dumper $thing;. Use\when you want to dump a variable that's not a scalar.