On 2010.04.13 23:17, Kenneth Wolcott wrote: > Hi; > > On Tue, Apr 13, 2010 at 19:54, Uri Guttman <u...@stemsystems.com> wrote: >>>>>>> "JG" == Jim Gibson <jimsgib...@gmail.com> writes: >> >> JG> On 4/13/10 Tue Apr 13, 2010 4:35 PM, "Mimi Cafe" >> <mimic...@googlemail.com> >> JG> scribbled: >> >> >> I think this will work, but is it elegant.? >> >> JG> Yes, it will work, and yes, it is elegant, as long as it encapsulates >> the >> JG> logic that is required by your program. >> >> 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. >> >> uri > > I really like the switch statement (native in Perl v5.10) over > anything more complicated than one if/else clause.
I agree with Uri. Even switch statements can be cumbersome depending on how many cases you have. I believe that (in the majority of cases) dispatch tables are far more effective, easy to read (like a table of contents) and maintainable (ie. very easy to add to without having to worry about placement). #!/usr/bin/perl use warnings; use strict; my $dt = { simple => sub { print "Simple, anon sub inline\n" }, easy => sub { my $num = 1; print $num*2 ."\n";}, complex => \&complex, # coderef to external sub, }; sub complex { my $num = shift; # ... do a bunch of stuff print "$num\n"; } # call them $dt->{ simple }(); $dt->{ easy }(); $dt->{ complex }( 5 ); Steve -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/