qbxk has asked for the wisdom of the Perl Monks concerning the following question:
Writing a function to traverse a "configuration" array I have a series of if/elsifs that switch based on what kind of data it encounters, ie. by whatever ref() returns.
It's not what you look like, when you're doin' what you’re doin'.
It's what you’re doin' when you’re doin' what you look like you’re doin'!
- Charles Wright & the Watts 103rd Street Rhythm Band
here's an mini-example:
my $ref = ref($_); if( $ref eq 'Regexp' ) { #ah, we are matching a match } elsif( $ref eq 'CODE') { #sombody slipped a sub{} in here, ok } elsif( $ref eq 'ARRAY') { #an @array we have found } elsif( $ref eq 'HASH') { #a %hash, obviously } elsif( $ref eq '') { #must be a $scalar } else { warn "Unknown ref() wth is a $ref ?!"; }
My question for the clergy is: what's a more elegant way to accomplish this? or what is your favorite idiom for this task?
also, have i hit on all the possible values that ref() can return?
It's not what you look like, when you're doin' what you’re doin'.
It's what you’re doin' when you’re doin' what you look like you’re doin'!
- Charles Wright & the Watts 103rd Street Rhythm Band
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Behaving appropriately based on ref() result
by esskar (Deacon) on Jan 08, 2006 at 14:46 UTC | |
by suaveant (Parson) on Jan 09, 2006 at 05:00 UTC | |
Re: Behaving appropriately based on ref() result
by tirwhan (Abbot) on Jan 08, 2006 at 14:55 UTC | |
Re: Behaving appropriately based on ref() result
by dragonchild (Archbishop) on Jan 08, 2006 at 16:22 UTC | |
by demerphq (Chancellor) on Jan 09, 2006 at 09:11 UTC | |
Re: Behaving appropriately based on ref() result
by McDarren (Abbot) on Jan 08, 2006 at 14:44 UTC | |
Re: Behaving appropriately based on ref() result
by demerphq (Chancellor) on Jan 09, 2006 at 09:04 UTC | |
by suaveant (Parson) on Jan 09, 2006 at 15:00 UTC | |
Re: Behaving appropriately based on ref() result
by bsb (Priest) on Jan 09, 2006 at 07:52 UTC | |
by demerphq (Chancellor) on Jan 09, 2006 at 08:46 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Back to
Seekers of Perl Wisdom