OT:Perl on Windows

John Macdonald john-Z7w/En0MP3xWk0Htik3J/w at public.gmane.org
Sat Mar 12 00:07:05 UTC 2005


On Fri, Mar 11, 2005 at 02:55:40PM -0500, Zbigniew Koziol wrote:
> John Macdonald wrote:
> >One additional note: if there is any chance that you'd ever
> >be copying a file that is large, it would be better to avoid
> >loading the entire file into memory before writing it back out.
> >
> >Change:
> >
> >    while (<TXT>) {
> >            $data .= $_;
> >    }
> >    
> >    print $data;
> >    
> >    print OUT $data;
> >
> >To:
> >
> >    while (<TXT>) {
> >        print $_;
> >        print OUT $_;
> >    }
> >
> 
> No... this doesn't matter. The first print will only allow to test if 
> input data are there.

Um, what doesn't matter? Running out of memory when you read
the entire (possibly huge) file into $data?  You don't need
the first print to determine that the program failed in that
case - on Linux you'll get a segfault, presumeably you get
some sort of failure indication on Windows too in such a case.
The useful thing is to avoid the failure, though, rather than
to get some sort of indication that it happened.

The original code printed the entire file both to the desired
destination and to stdout, my code duplicated that.  Are you
saying that the form of the program could be changed to only
print the first line of the input file?

> One may also just write
> 
>          print;
>          print OUT;
> 
> when using $_.

That's true.

> And a one-liner version I posted before (its different a bit from the 
> original one of William, since file names are define as STDIN and STDOUT 
> on command line):
> 
> perl -e 'while(<>){ print; }' < input.txt > aoutput.txt

This does the same line by line copying that you said doesn't
matter above.  I guess you're really saying that copying to
stdout as well as to the destination file wasn't necessary.
That's the original programmer's decision to make - perhaps
he really wanted to same sort of functionality as the Unix
tee program or perhaps it was there for debugging and would
have been removed as soon as things were working.

If you really want to turn it into a one liner (which might not
be appropriate - the original may have been scaled down from
a much bigger set of requirements, just to get the first step
working), the -p flag would make it trivial:

    perl -pe '' input.txt > output.txt

-- 
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list