mshaw has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; use Carp; my %h = ( 'one' => 'uno', 'two' => 'dos', 'three' => 'tres', ); my $s = 'foo bar'; sub xyz() { my $arg; my $argNum=0; my $stringOrRef; while(defined($arg = shift)) { if($argNum == 0) { $stringOrRef = $arg; } else { carp "one arg only please"; } $argNum++; } # here is where I test the argument ###################################### if($stringOrRef =~ /^HASH\(/) { print "it's a hash:\n"; foreach my $k (keys %{ $stringOrRef }) { print " $k : $$stringOrRef{$k}\n"; } } else { print "it's a string:\n"; print " $stringOrRef\n"; } } &xyz($s); &xyz(\%h);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to tell if I have a string, or a ref to a hash
by moritz (Cardinal) on Oct 19, 2010 at 16:40 UTC | |
|
Re: How to tell if I have a string, or a ref to a hash
by Corion (Patriarch) on Oct 19, 2010 at 16:38 UTC | |
|
Re: How to tell if I have a string, or a ref to a hash
by ikegami (Patriarch) on Oct 19, 2010 at 17:36 UTC | |
by mshaw (Initiate) on Oct 19, 2010 at 18:06 UTC | |
by jakeease (Friar) on Oct 19, 2010 at 18:44 UTC | |
|
Re: How to tell if I have a string, or a ref to a hash
by chromatic (Archbishop) on Oct 19, 2010 at 18:42 UTC | |
|
Re: How to tell if I have a string, or a ref to a hash
by locked_user sundialsvc4 (Abbot) on Oct 19, 2010 at 18:17 UTC |