http://www.perlmonks.org?node_id=521822


in reply to Behaving appropriately based on ref() result

use a Hash
my %RefTypes = ( 'Regexp' => \&HandleRegexp, 'CODE' => \&HandleCodeRef, 'ARRAY' => \&HandleArrayRef, 'HASH' => \&HandleHashRef, 'SCALAR' => \&HandleScalarRef, '' => \&HandleScalar, ); my $ref = ref($_); if(exists $RefTypes{$ref}) { $RefTypes{$ref}->($_); } else { warn "Unknown ref() wth is a $ref ?!"; }
HTH

Replies are listed 'Best First'.
Re^2: Behaving appropriately based on ref() result
by suaveant (Parson) on Jan 09, 2006 at 05:00 UTC
    Just for reference using a hash this way is called a dispatch table.

                    - Ant
                    - Some of my best work - (1 2 3)