sub write_vars { my $filename = shift(@_); my $vars = shift(@_); open(FILE, ">$filename"); foreach my $key (keys %{$vars}) { my $i = \( $vars->{$key} ); # this is a switch statement # make a reference to $vars->{$key} and pass it to ref for ( ref $i ) { if (/SCALAR/) { print $key . " = " . $vars->{$key} . "\n"; } else { $j = $vars->{$key}; $type = ref $j; print $type . "\n"; if ($type eq "ARRAY") { for my $k ( @{$j} ) { print $k . "\n"; } } elsif ($type eq "HASH") { for my $l ( keys %{$j} ) { print $l . ": " . $j->{$l} . "\n"; } } } } } close(FILE); return; } %a = qw(One 1 Three 3); %foo = ('a', \%a, 'b', 'B', 'c', 'C'); write_vars("test.txt", \%foo);