From: "John W. Krahn" <[EMAIL PROTECTED]>
> Jenda Krynicky wrote:
> > Let's write it as an ordinary program:
> > 
> > #!perl -w
> > use strict;
> > 
> > sub CleanupLog {
> >     my ($from_file, $to_file) = @_;
> >     open my $IN, '< ' . $from_file
> 
> Since you are using lexically scoped file handles we can also use the
> three argument form of open.

I'm still not used to it :-)
Maybe it's be safer (in case someone needs to run the code in an 
older Perl) to use

        use FileHandle;
        my $IN = new FileHandle;
        open $IN, '< ' . $from_file;
 
>      open my $IN, '<', $from_file
> 
> >         or die "Can't open file $from_file : $!\n";
> >     open my $OUT, '> ' . $to_file
> >         or die "Can't create file $to_file : $!\n";
> >     my $line = '';
> >     while (<$IN>) {
> >         chomp; # get rid of the newline
> >         next if $_ eq ''; # skip empty lines
> 
> What happens if $_ eq '   '?

You are right.
        next if /^\s*$/;

> >         s/\s{2,}/ /g; # replace 2 or more whitespace chars
> >                       # with a single space
> 
> Or simply:
> 
>           s/\s+/ /g;

I think mine is a bit quicker. Though ... they do not mean the same.
Mine will leave a single tab alone, but convert two tabs to a single 
space. Therefore maybe your is better.

> Or for speed:
> 
>           tr/ \t\r\n\f/ /s;

Yeah, I keep forgetting about tr//s.

> >         if ( m{^\d{4}/\d{2}/\d{2} \d\d:\d\d:\d\d} ) {
> >                       # if the current line starts with a timestamp
> >                       # ... I assume the YYYY/MM/DD HH:MI:SS format
> >             print OUT $line, "\n" if $line;
>                     ^^^
> Unknown file handle?

A typo. I fixed it in the tested code, but forgot to "backfix" it in 
the email.

> >                       # print the buffer
> >             $line = $_;
> >                       # remember the current line
> >         } else {
> >             $line .= ' ' . $_;
> >                       # add the current line to the buffer
> >         }
> >     }
> >     print $OUT $line, "\n" if $line;
> >             # print the last buffer
> >     close $IN;
> >     close $OUT;
> > }
> 
> > # now let's process all .log files in the current directory and
> > # write the results to 'Cleaned' subdirectory
> > mkdir 'Cleaned', 0777 unless -d 'Cleaned';
> > opendir my $DIR, '.';
> 
> You should _always_ verify that the directory was opened.
> 
> opendir my $DIR, '.' or die "Cannot opendir '.': $!";

You're right.

Thanks for pointing out the mistakes and typos :-)

Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to