[GRLUG] Add files matching a pattern to an array

Al Tobey tobert at gmail.com
Thu Nov 16 16:25:16 EST 2006


perldoc File::Find
http://search.cpan.org/~nwclark/perl-5.8.8/lib/File/Find.pm

http://grand-rapids.pm.org (shameless plug)

This is not tested at all.    File::Find is a bit old-school because
it uses package globals, but it's not as bad as your CS professor
would make it out to be.

File::Copy has a nice move() function which is smarter than rename().
 rename() does not work across filesystem boundaries, although I don't
think that's a problem in your example.

You may want to adjust the first test in the wanted() function to
match against your array of files.     I switched to File::Find
because it looks like you want to do wildcard matching of sorts.
The @FILES = qw( file.txt[0-9] ... ); thing won't work, since qw()
does literal quoting, like quotes, but splits on whitespace for you.
Perhaps m/(files|anotherfile).txt[0-9]+/ is what you want to test on.
  Or if it's a static list:

@FILES = qw( file1.txt file2.txt anotherfile.txt foo.bar );
sub wanted {
    return unless grep {$_ eq $File::Find::name} @FILES;
    ...
}

But if it's a static list, you could/should then skip File::Find if
it's a hard-coded list. Some people prefer to use File::Find to build
an array of files then loop over that list to process the files' data.
   TMTOWTDI! (http://catb.org/jargon/html/T/TMTOWTDI.html)

# loop over a list of files
foreach my $f ( @FILES ) {
    open( my $in, "< $f" );
    open( my $out, "> $f.tmp" );
    ...
    rename( "$f.tmp", $f );
}

#!/usr/bin/perl

use strict;
use warnings;
use File::Find;
use File::Copy;

find( \&wanted, '.' );

sub wanted {
    # only process .txt files - modify this regular expression to
taste (perldoc perlre)
    return unless ( $File::Find::name =~ /\.txt$/ );

    open( my $in, "< $File::Find::name" );
    open( my $out, "> $File::Find::name.new" );

    while ( my $line = <$in> ) {
        # do stuff with $line
        print $out $line;
    }

    close( $out );
    close( $in );

    move( "$File::Find::name.tmp" $File::Find::name );
}

__END__

On 11/16/06, Justin Denick <justin.denick at gmail.com> wrote:
> I would like to use Perl to open an array of files individually
>
> !#/usr/bin/perl
> use strict;
> user warnings;
>
> my $source;
> my @FILES = qw(file.txt?[0-9] anotherfile.txt);
>
> #There may be several versions of the file ' file.txt' and each is denoted
> by a digit after the file extension.
> ## file.txt
> ## file.txt1
> ## file.txt2
>
>
> for $source (@FILES) {
>      my $destination = "$source.tmp";l
>      open IN, "$source" or die $!;
>      open OUT, "> $destination or die $!;
>      while (<>) {
>           <code DEFANGED_to DEFANGED_execute>
>      }
> rename $destination, $source;
> }
>
> My Perl skills are not yet fully developed to please bear with my ignorance.
>
> --
> In vino veritas.
>         [In wine there is truth.]
>                 -- Pliny
> _______________________________________________
> grlug mailing list
> grlug at grlug.org
> http://shinobu.grlug.org/cgi-bin/mailman/listinfo/grlug
>
>


More information about the grlug mailing list