On Tue, Sep 03, 2002 at 04:33:37PM -0700, Pam Derks wrote:

[snip]
 
> #!/usr/bin/perl
> #process all files in directory www/htdocs
> 
> use File::Find;
> @ARGV = qw(/dept/unxmkt/www/htdocs) unless @ARGV;
> find(\&find_it, @ARGV);
>  
>    sub find_it{
>        foreach $_(@ARGV){
>              while (<>){

This is your problem.  From your code it appears you think <> causes the
filename in $_ to be opened and read line-by-line, or perhaps (because $_ is
a directory) that it reads all of the files in the directory in $_.  Neither
is the case, see below.


>                    if ($_=~ /Take our survey/){
>                          print ("$_\n");
>                    } 
>              } 
>        }
>  
>   } #end find_it


File::Find already gives you the name of the file, so you don't need to
iterate over @ARGV in the find subroutine.  Also, looping over <> loops over
@ARGV, attempting to open each element as if it were a filename, then
reading each line.  Your @ARGV contains directory names, so the <> won't
work very well.

This is the code I expect you were trying for (with a few additions):

    #!/usr/bin/perl -w
    
    use File::Find;
    use strict;
    
    
    @ARGV = qw(/dept/unxmkt/www/htdocs) unless @ARGV;
    find(\&find_it, @ARGV);
    
    sub find_it {
        return unless -f;
    
        unless (open(FILE, $_)) { 
            warn("Unable to open file \"$File::Find::name\": \l$!.\n");
            return;
        }
    
        while (<FILE>) {
            if (/Take our survey/) {
                print "$File::Find::name\n";
                return;
            }
        }
    }


The code is indented an extra four spaces; you will have to remove the
indentation for the first line, at least, for it to work.

Did you intend for the filename to be printed for files that matched, or
each line that matched?  If you meant for the filename to be printed, then
the above code is correct.  If you meant for each line to be printed, then
you'll have to change the code in the /Take our survey/ block.  I'll leave
this as an exercise for you, but let us know if you're still confused.

Always use strict and -w in your code, they very well might have pointed out
problems with your original.

You may want to add an 'i' option to the regex, e.g. /Take our survey/i.

If you have GNU grep on your system the above code can be accomplished with
grep -lr 'Take our survey' /dept/unxmkt/www/htdocs.


Let us know if that solves your problems, or if you're still confused.


--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

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

Reply via email to