#!/usr/bin/perl -w
use strict;
use warnings;
my %xxx = (a=>5, b=>10, c=> {ca=>9, cb=>10});
my @yyy = ( [0, 1], {red=>2, blue=>3} );
my $aa = 5; my $bb=\%xxx; my $cc=\@yyy;
print "aa=", thing_to_string($aa), "\n";
print "bb=", thing_to_string($bb), "\n";
print "cc=", thing_to_string($cc), "\n";
sub thing_to_string {
my $t = shift;
return '???UNDEF???' if !defined $t;
if (ref $t eq '') {
# It's a scalar, return it's value
return $t;
}
elsif (ref $t eq 'ARRAY') {
# It's an array, turn all the elements into strings,
# separate them with commas, and wrap 'em in '(' and ')'
return "(".join(", ", map { thing_to_string($_) } @$t ).")";
}
elsif (ref $t eq 'HASH') {
# It's a hash, turn all the elements into "key:value",
# separate them with commas, and wrap 'em in '{' and '}'
return "{".join(", ", map { $_."=>".thing_to_string($$t{$_}) }
sort keys %$t)."}";
}
# some other thing...
return "...other...";
}
####
[10:41:17] ~
$ perl -e '$a=5; print ref($a)'
[10:41:29] ~
$
####
[10:41:29] ~
$ perl -e '$a=5; print ref(\$a)'
SCALAR
[10:43:19] ~
$ perl -e '@a=(1,2); print ref(\@a)'
ARRAY
[10:43:23] ~
$ perl -e '%a=(1=>0,2=>0); print ref(\%a)'
HASH
[10:43:26] ~
$