Harry Putnam wrote: > "Uri Guttman" <u...@stemsystems.com> writes: > >> i disagree that it is elegant. too often if/else lists >> are not >> needed. many can be replaced by dispatch tables. if one >> of the clauses >> does just a return or next/last that can be replaced >> with a modifier or >> shorter statement. without ANY serious work, i have over >> 10k lines of >> perl code in one system with about 10 else's and maybe 3 >> elsif's. it >> just is a matter of knowing how to manage flow control >> well and you >> rarely need else's. > > Can someone show an example of an if/elsif/else nested > construct being > replaced by a dispatch table? > > --
Here's an example I gave in a similar question in another forum. my %dispatch = ( 1 => \&getcpuinfo, 2 => \&osversion, 3 => \&loadaverages, 4 => \&systemload_uptime, 5 => \&netinterfaceinfo, 6 => \&diskusage, 7 => \&ipaddress, q => sub { print "Goodbye\n" and exit; }, error => sub { print "invalid selection\n" }, ); while(1) { print "press 1 to get CPU Info \n", "press 2 to get OS version \n", "press 3 to get CPU Load averages\n", "press 4 to get System Load & Uptime\n", "press 5 to get Net Interface info\n", "press 6 to get system disk usage info \n", "press 7 to get IP address info \n"; "press q to Exit\n" chomp(my $selection) = <STDIN>; my $code = $dispatch{$selection} || $dispatch{'error'} ; $code->(); } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/