in reply to Comparing references to sub's
mmmm... so your lower level sub tells you which function to call, but at a higher level you want to know which one it chose, so you can do some other stuff?
Perhaps it would be better to code it using sub NAMES rather than refs?
#!/usr/local/bin/perl -w use strict; no strict qw(refs); my $food = $ARGV[0]; $food ||= 'apple'; my $subname = choose($food); print "We got '$subname' - let's get cookin!\n"; &$subname($food); if ( $subname eq 'fruit' ) { print "Puddings up!\n"; } #--------------- sub choose { my $food = shift; return 'fruit' if ( $food eq 'apple' ); return 'vege' if ( $food eq 'carrot' ); return 'unknown'; } sub fruit { my $food = shift; print "I think '$food' is FRUIT!\n"; } sub vege { my $food = shift; print "I think '$food' is VEGE!\n"; } sub unknown { my $food = shift; print "I don't think '$food' is food at all!\n"; }
Then you are working with sub names, and it is clear in your code when you say thinks like:
if ( $subname eq 'do_zipped_things' )...
My 0.02 dollars, regards Jeff
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Comparing references to sub's
by leriksen (Curate) on Mar 24, 2003 at 03:41 UTC |