On Mon, 18 Feb 2002 14:40:17 -0900, [EMAIL PROTECTED] (Michael Fowler) wrote:

>On Mon, Feb 18, 2002 at 07:20:50AM -0500, zentara wrote:
>> You are using nested while loops to iterate
>> thru a file, this isn't necessary. Also you don't need to
>> open a file just to loop thru it.
>
>Yes, you do.  At some level, you have to open the file if you want to
>read its contents.
>
>
>> my $file  = 'c:/davidcode/perlbeast/*.secret';
>> while (<$file>){
>> print "$file: $.  :$_\n"  if  /$regexp/;
>> }
>
>This code doesn't actually work.  Did you test it?  Perl won't automatically
>open a file using the file read operator like that.  The <> operator is the
>only one that's special in that respect.
Ooops, you can tell I'm a beginner.  I was taking it from some code like below.
I forgot about the magical properties of @ARGV to auto-open files.
I just mindlessly stuck $file in there for ARGV.  My apologies.
I know, I know.......always test your code.

while (<>){
 print "$file: $.  :$_\n"  if  /$regexp/;
 }


#!/usr/bin/perl
use warnings;
use strict;
use File::Find;
my ($search, $ext) = @ARGV;
if (defined $ext) {$ext = ".$ext"} else {$ext = '.*'};
die "Usage : greprz 'search' 'extension' (extension optional)\n" if ($search eq
"");
@ARGV = ();
find (sub { push @ARGV, $File::Find::name if (-f and -T and  $ext or
/\Q$ext$/)}, ".");
while (<ARGV>) {
close ARGV if eof;
print "$ARGV: $. :$_\n" if /$search/;
    }
    
    
    


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

Reply via email to