On Monday 03 December 2007 10:34, [EMAIL PROTECTED] wrote: > > John W.Krahn <[EMAIL PROTECTED]> writes: > > > > If you want to incorporate the grep into the perl program then this > > may work (UNTESTED): > > It works with 1 change and one caveat, The cavaet is that the file > names in $ARGV must be absolute format or the program fails.
The following program does not use the $ARGV variable, nor does Perl use it. > That > has something to do with File::Finds builtin of cd ing to the source > directory I think. ??? > Johns program; with a few questions and annotations: > (stripping the normal reply (>) indicators: > > #!/usr/bin/perl > use warnings; > use strict; > > use File::Find; > > ( my $myscript = $0 ) =~ s!\A.*/!!; > > @ARGV == 3 or die "usage: $myscript 'REGEX' SOMEDIRECTORY > TARGETDIRECTORY\n"; > > my $regex = qr/$ARGV[0]/; > > my ( $SrcDir, $TrgDir ) = @ARGV[ 1, 2 ]; > > ## [HP I would have thought it would cause perl to still treat ARGV > as ## file names since its value is not zeroed out by the above ## > operations, but apparently that is not a problem > ## ] @ARGV is just an array, you can process its contents in whatever way you like. The contents are assumed to be file names when you use the special <> readline operator. > my ( $OldCnt, $Num ); > > ## [HP $Num needs to be set to zero because this section > ## will eventually throw this error: > ## Use of uninitialized value in numeric lt (<) at ./Krahn.pl line 20 > ## if it isn't OK, change that line to: my ( $Num, $OldCnt ) = 0; > $Num = 0; > > ## ] > > { opendir my $dh, $TrgDir or die "Cannot open '$TrgDir' $!"; > while ( my $file = readdir $dh ) { > next unless -f "$TrgDir/$file" and $file =~ /\A\d+\z/; > $Num = $file if $Num < $file; > $OldCnt++; > } > } > > ## [ I don't really understand the format of having the `open' and > ## processing enclosed in {}. Don't think I've noticed that before.. > ## But the clause is nice and tidy.. thanks > ## ] It is used to limit the scope of the lexical variables, particulary the directory handle $dh which will be closed automatically when it goes out of scope. > my $CopyCnt; > find sub { > return unless -f and /\A\d+\z/; > open my $fh, '<:raw', $_ > or die "Cannot open '$_' $!"; > my $size = -s $fh; > $size == read $fh, my $data, $size > or die "Cannot read '$_' $!"; > return unless $data =~ $regex; > > ## [HP I don't really understand how `return' operates here. Is it > ## doing the same job as `next'? > ## ] 'next' works inside loops. Because this is inside a subroutine you have to use 'return'. > $Num++; > open my $out, '>:raw', "$TrgDir/$Num" > or die "Cannot open '$TrgDir/$Num' $!"; > print $out $data > or die "Cannot print to '$TrgDir/$Num' $!"; > $CopyCnt++; > }, $SrcDir; > > ## [ I don't really understand why some of the processing is done. I > ## hadn't seen `:raw' used before but apparently the :raw part is > ## there to handle the possiblitiy of different line endings in the > ## files. Then size is checked; apparently to ensure the size > reported ## in -s is the same when `read' Correct. > ## And then the data is `printed' to its new home instead of just > ## being copied there.... why is that being done? It is being copied, since all the data from the old file is in the $data variable it is copied to the new file. > ## ] HTH John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/