Congratulations.  When you get a bit further on you'll discover how to use 
modules for XML work, but there's nothing wrong with what you're doing 
right now.

I'll just pick on one part here and let others go for the rest:

At 03:33 PM 5/24/01 +0000, Stout, Joel R wrote:
>sub testType {
>  if (/^BIG/) {
>   parseBIG ($_[0]);
>  } elsif (/^REF/) {
>   parseREF ($_[0]);
>  } elsif (/^TDS/) {
>   parseTDS ($_[0]);
>  } elsif (/^CAD/) {
>   parseCAD ($_[0]);
>  } elsif (/^ISS/) {
>   parseISS ($_[0]);
>  } elsif (/^CUR/) {
>   parseCUR ($_[0]);
>  } elsif (/^NTE/) {
>   parseNTE ($_[0]);
>  } elsif (/^N1/) {
>   parseN1 ($_[0]);
>  } elsif (/^N3/) {
>   parseN3 ($_[0]);
>  } elsif (/^N4/) {
>   parseN4 ($_[0]);
>  } elsif (/^AMT/) {
>   parseAMT ($_[0]);
>  } elsif (/^ITD/) {
>   parseITD ($_[0]);
>  } elsif (/^PKG/) {
>   parsePKG ($_[0]);
>  } elsif (/^R4/) {
>   parseR4 ($_[0]);
>  } elsif (/^IT1/) {
>   parseIT1 ($_[0]);
>  } elsif (/^PID/) {
>   parsePID ($_[0]);
>  } elsif (/^SE/) {
>   printResults();
>  }

Any time you find yourself doing something that feels mindless or 
uncreative, that's a clue that there's a way for Perl to make life easier 
for you.  In this case, observe the considerable repetition.  You could 
simplifiy this using a function dispatch table (you'll really get a kick 
out of references when you learn them):

sub testType {
     my %dispatch = (BIG => \&parseBIG,
                     REF => \&parseREF,
                     TDS => \&parseTDS,
                     CAD => \&parseCAD,
                     ISS => \&parseISS,
                     CUR => \&parseCUR,
                     NTE => \&parseNTE,
                     N1  => \&parseN1,
                     N3  => \&parseN3,
                     N4  => \&parseN4,
                     AMT => \&parseAMT,
                     ITD => \&parseITD,
                     PKG => \&parsePKG,
                     R4  => \&parseR4,
                     IT1 => \&parseIT1,
                     PID => \&parsePID);
     my $prefixes = join '|', keys %dispatch;
     if (/^($prefixes)/) {
         $dispatch{$1}->(shift);
     }
     elsif /^SE/ {
         printResults();
     }
}

This is vulnerable if you put in more strings where one is a prefix of 
another, though, just be aware of that.

Actually, given your naming structure, it would be possible to make it even 
shorter using even more magic, and avoid that potential ordering problem at 
the same time:

sub testType {
     my $parse = "BIG|REF|TDS|CAD|ISS|CUR|NTE|N1|N3|N4|AMT|ITD|PKG|R4|IT1|PID";
     if (/^($parse)/) {
         my $subname = "parse$1";
         goto &$subname;
     }
     elsif (/^SE/) {
         printResults();
     }
}

but that's a bit unfair to you right now :-)

Oh, and add a -w to your shebang line.
--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com

Reply via email to