http://qs321.pair.com?node_id=99943

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

The code at bottom works fine for converting all alpha characters in a text file to upper (or lower) case.   But having the two "$munged = " lines inside the "for(@in)" loop looks like ineficient if handling large files.

I tried these two tweaks to move that logic outside the for(@in) loop.   The first doesn't work because (correct me if I'm wrong) $_ is empty when $munged is declared.   The second fails with this compilation error: syntax error at uclc.pl line (push @munged, $munged();), near "$munged(" and I'm not clear just what's happening there.

# tweak one my @munged; my $munged = lc() if ($uclc eq 'lc'); $munged = uc() if ($uclc eq 'uc'); for(@in) { push @munged, $munged; } # tweak two my @munged; my $munged = lc if ($uclc eq 'lc'); $munged = uc if ($uclc eq 'uc'); for(@in) { push @munged, $munged(); }
So my questions for the wise bretheren and sisteren are:
  1. should I be concerned about potential ineficiency of this code for large input files?
  2. if so, how might it be improved?
    cheers,
    Don
    striving toward Perl Adept
    (it's pronounced "why-bick")
#!/usr/bin/perl -w use strict; my $uclc = shift or Usage(); my $infile = shift or Usage(); my $outfile = shift or Usage(); Usage() unless ($uclc eq 'lc' or 'uc'); open (IN, "< $infile") or die "Error opening $infile for read: $!"; my @in = <IN>; close IN or die "Error closing $infile after read: $!"; my @munged; for(@in) { my $munged = lc() if ($uclc eq 'lc'); $munged = uc() if ($uclc eq 'uc'); push @munged, $munged; } open (OUT, "> $outfile") or die "Error opening $outfile for write: $!" +; print OUT for(@munged); close OUT or die "Error closing $outfile after write: $ +!"; ###################################################################### +#### sub Usage { die "\n Usage: uclc.pl (lc|uc) infile outfile\n"; } ###################################################################### +####