On Wed, Apr 17, 2002 at 02:02:02PM -0400, Bill -Sx- Jones wrote:
> I have the habit of doing:
> 
>          last if (substr($vFlag, 1, 3) eq 'END');
> $vSub = \&Sneex   if (substr($vFlag, 1, 5) eq 'SNEEX');
> $vSub = \&Admin   if (substr($vFlag, 1, 5) eq 'ADMIN');
> $vSub = \&Reports if (substr($vFlag, 1, 7) eq 'REPORTS');
> $vSub = \&Logs    if (substr($vFlag, 1, 4) eq 'LOGS');
> $vSub = \&Targets if (substr($vFlag, 1, 7) eq 'TARGETS');
> $vSub = \&Usenet  if (substr($vFlag, 1, 6) eq 'USENET');
> 
> (substr($_, 0, 1) eq '[') ? next : &$vSub;
> 
> 
> (NOTE:  Obviously NOT all of the 668 lines of program code
> is here, so this doesn't stand on it's own two feet...)
> 
> 
> Q: Is there a better way?

The above implies the format is something like:

    SNEEXADMINEND

in which case, suicide is an honorable option.

Adding in some sort of delimiter:

    SNEEX|ADMIN|END

and then you can just split /\|/.


Either way, use a dispatch table.  Here's the example using the ugly
format.

    my $End = 0;
    sub End { $End = 1; }

    my %dispatch = (
        SNEEX       => \&Sneex,
        ADMIN       => \&Admin,
        REPORTS     => \&Reports,
        END         => \&End,
        ...
    );

    while( length $vFlag && !$End ) {
        DISPATCH: foreach my $flag (keys %dispatch) {
            if( substr($vFlag, 0, length $flag) eq $flag ) {
                $dispatch{$flag}->();
                last DISPATCH;
            }
        }
    }

something like that.


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>    http://www.pobox.com/~schwern/
Perl Quality Assurance      <[EMAIL PROTECTED]>         Kwalitee Is Job One
The House of Representatives does hereby authorize the notification of
the casino riverboat consulate of this Resolution and impending
whoopin' so that they may remove their casino vessels to friendlier
waters.
    -- Kentucky Legislature, HR 256 encouraging the purchase of
       a nuclear attack submarine.

Reply via email to