[Israel.pm] Detecting Scalar type?
Yuval Kogman
nothingmuch at woobling.org
Fri Jul 25 16:39:53 EEST 2008
On Fri, Jul 25, 2008 at 12:38:49 +0000, Mikhael Goikhman wrote:
> for my $thing (@things) {
> my $type = ref($thing);
> # do something accourding to $type, for example, just print it
> print defined $thing ? "$thing is of type '$type'\n" : "undef\n";
> }
Usually what I like to do is:
sub handle_thing {
my ( $self, $thing ) = @_;
my $type = ( blessed($thing) ? "object" : ( ref($thing) || "scalar") );
my $method = $self->can("handle_$type") || "handle_fallback";
$self->$method($thing);
}
and then you just add 'handle_object', 'handle_hash',
'handle_scalar' etc. See Data::Visitor or Devel::PartialDump for an
example
--
Yuval Kogman <nothingmuch at woobling.org>
http://nothingmuch.woobling.org 0xEBD27418
More information about the Perl
mailing list