To help with debugging I wrote showvar() to display a variable's name and type. This routine then calls show() or showHASH() to display the element count and content. A poor man's DUMPER. It works fine for my needs.

To cut down on typing, I would like to change:

showvar('$x',\$x) to showvar($x) showvar('@x',\@x) to showvar(@x) showvar('%x',\%x) to showvar(%x)
and still get the same results.

If we pass just a single argument named sigilVarname, say showvar(sigilVarname), what has got me stumped is how to extract the "Varname" part.

ie. showvar(@hello) sigil==@, Varname==hello

Any ideas on how to rewrite showvar() so that it works with one argument?

$x = qq(odd number of items kaboom); showvar('$x',\$x); # prints # $x SCALAR, 1 ITEM: # 'odd number of items kaboom' @x = qw(odd number of items kaboom); showvar('@x',\@x); # prints # @x ARRAY, 5 ITEMS: # 'odd' # 'number' # 'of' # 'items' # 'kaboom' %x = qw(odd number of items kaboom); showvar('%x',\%x); # prints # %x HASH, 3 KEY/VALUE PAIRS: # kaboom => ***EMPTY*** # odd => number # of => items sub showvar { # variables $, @, % local ($varname, $_) = @_; return print "\n*** One or two arguments are undefined ***\n" if ! +$_[0] or !$_[1]; my $type = ref; return print "\nVariable '$varname' is inconsistent with its refer +ence\n" if !$type; if (/SCALAR/) {print "$varname $type, "; show ($$_)} elsif (/ARRAY/) {print "$varname $type, "; show (@$_)} elsif (/HASH/) {print "$varname $type, "; showHASH (%$_)} else {print "$varname $type, is not of type SCALAR/ARRA +Y/HASH\n"} } sub show { my $len = scalar @_; my $plural = $len>1 ? 'ITEMS' : 'ITEM'; $_[0]="***EMPTY***" unless $_[0]; #avoid undef in mapping below print join "\n", "$len $plural:", map(" '$_'", @_), "------------ +\n\n"; } sub showHASH { . . # Avoid length error if hash value undef $hash{$_} = '***EMPTY***' if !defined $hash{$_}; . . # Pretty print print scalar keys %hash, " KEY/VALUE PAIRS: \n"; . . }

In reply to How to extract the name of a variable? by Anonymous Monk

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.