Consider:

use strict; use warnings; use Tk; my $mw = MainWindow->new (-title => 'Hello World'); my @arr=('first', 'second', 'third'); my %hash = (A => 100, B => 200, C => [@arr], D => \&hi, E => $mw); $hash{'A'}=100; $hash{'B'}=200; $hash{'C'}=[@arr]; for (sort keys %hash) { print "$_ => "; if (ref($hash{$_}) eq 'ARRAY') { print "Array: ", join(', ', @{ $hash{$_} }); } elsif (ref($hash{$_}) eq 'HASH') { # do something else print "Hash: ..."; } else { print "Scalar: $hash{$_}"; } print "\n"; } sub hi { print "hello World"; }

Prints:

A => Scalar: 100 B => Scalar: 200 C => Array: first, second, third D => Scalar: CODE(0x21bdf50) E => Scalar: MainWindow=HASH(0x21d97ac)

Adding:

elsif (ref($hash{$_})) { print "Unknown ref: $hash{$_}";

alters the output to:

A => Scalar: 100 B => Scalar: 200 C => Array: first, second, third D => Unknown ref: CODE(0x21be538) E => Unknown ref: MainWindow=HASH(0x21d9e38)

References come in many flavours. There is probably no good general answer to OP's question for "display" output (as opposed to debugging output), but at least unhandled cases can be caught by noting that ref returns undef for non-reference values.


DWIM is Perl's answer to Gödel

In reply to Re^2: Complex hash? by GrandFather
in thread Complex hash? by tamaguchi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.