I thought of it as a method to avoid a really long list of#!/usr/bin/perl use warnings; use strict; my %options = ( one => \&one, two => \&two, three => \&three, ); my $ret; # return code from the sub while(chomp(my $opt = <STDIN>)){ if(exists $options{$opt}) { $ret = &{$options{$opt}}; last if $ret == 0; } else { default(); } } sub one { print "one\n"; return 1; } sub two { print "two\n"; return 1; } sub three { print "three and break the loop\n"; return 0; } sub default { print "Default\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hash option/menu loop wierd or usefull?
by friedo (Prior) on Dec 07, 2005 at 17:42 UTC | |
by mulander (Monk) on Dec 07, 2005 at 17:47 UTC | |
by Limbic~Region (Chancellor) on Dec 07, 2005 at 18:14 UTC | |
by grinder (Bishop) on Dec 08, 2005 at 12:40 UTC | |
|
Re: Hash option/menu loop wierd or usefull?
by Aristotle (Chancellor) on Dec 26, 2005 at 09:59 UTC |