Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have alot of conditions in a function and would like to know if it can be shortened and made more efficient with the help of Perl experts. The script works on my NT but want to see if it can be shortened and look any better?
use strict; use warnings; my $macf = 0; my $syman = 0; #intialize all the other variables etc... #start of mainfunction here sub mainfunction(); { if(($macf == 0) && ($syman == 0)) { if(($mac eq $oldMac) && ($sym eq $oldSym)) { print "no changes\n"; } elsif(($mac ne $oldMac) && ($sym ne $oldSym)) { print "section C UPDATE\n"; } elsif($mac ne $oldMac) { print "section B UPDATE\n"; } elsif($sym ne $oldSym) { print "SEction E updated\n"; } } elsif(($macf == 0) && ($syman != 0)) { if($mac ne $oldMac) { print "MacSEction updated\n"; } else { print "no changes to mac\n"; } } elsif(($macf != 0) && ($syman == 0)) { if($sym ne $oldSym) { print "syman update\n"; } else { print "no changes to syman\n"; } } } #end of main function #start of condition checks to call correct function if(($syman == 0) && ($macf == 0)) { myfunc1(); myfunc2(); mainfunction(); } elsif(($syman == 0) && ($macf != 0)) { myfunc1(); mainfunction(); } elsif(($macf == 0) && ($syman != 0)) { myfunc2(); mainfunction(); }

Replies are listed 'Best First'.
Re: Shorten function conditions
by Roy Johnson (Monsignor) on Feb 02, 2004 at 20:50 UTC
    Nesting conditions will help you avoid retesting the same conditions, though you may have to look harder to see what branch you're following. I think I've kept all the logic from your main section.
    if ($macf == 0) { if ($syman == 0) { if($mac eq $oldMac) if ($sym eq $oldSym) { print "no changes\n"; } else { print "SEction E updated\n"; } } elsif ($sym ne $oldSym) { print "section C UPDATE\n"; } else { print "section B UPDATE\n"; } } elsif ($mac ne $oldMac) { print "MacSEction updated\n"; } else { print "no changes to mac\n"; } } elsif ($syman == 0) { if ($sym ne $oldSym) { print "syman update\n"; } else { print "no changes to syman\n"; } }

    The PerlMonk tr/// Advocate
      Roy, Thanks for your time and your script!
Re: Shorten function conditions
by Fletch (Bishop) on Feb 02, 2004 at 21:27 UTC
Re: Shorten function conditions
by welchavw (Pilgrim) on Feb 03, 2004 at 03:57 UTC

    Just an aside of no value, but you really freaked me out here as I started thinking about weakest preconditions and I had thought that purged from my gray matter! Good luck with your problem...looks like you've received some good help.

    ,welchavw

Re: Shorten function conditions
by simonm (Vicar) on Feb 03, 2004 at 22:09 UTC
    I would be inclined to approach code like that using a "table-driven" approach.

    Naturally, this being Perl, we can use idiomatic techniques with hashes and regexes rather than a by-the-book C-ish implementation.

    An (untested) stab at your problem might look like this:

    # Keys in %rules are ! $macf, ! $syman, eq $oldMac, eq $oldSym my %rules = ( '1111' => 'no changes', '1100' => 'section C UPDATE', '1101' => 'section B UPDATE', '1110' => 'SEction E updated', '100.' => 'MacSEction updated', '101.' => 'no changes to mac', '01.0' => 'syman update', '01.1' => 'no changes to syman', ); sub mainfunction { my $state = join('', map { $_ ? 1 : 0 } ($macf == 0), ($syman == 0), ($mac eq $oldMac), ($sym eq $oldSym) ); if ( my $rule = ( grep { $state =~ $_ } keys %rules )[0] ) { my $result = $rules->{$rule}; print $result . "\n"; } }