Melis Mutlu <[EMAIL PROTECTED]> wrote:

: Here is the problem:

    This belongs in a new thread. Start a new message to the
list that is not a reply. Knock off all this leading space.
It is unnecessarily wrapping lines. If you need to reply to
this message, only quote the relevant parts of the message.
Not the whole darn thing.


:             I want to search for a string in a directory. I
:             copy-pasted whole program as below:
:             What program does is, . . simply nothing. It returns
:             nothing. Can anybody see why? Can it be related to a
:             problem with the file test?

    Or the files aren't opening. You're not testing
for a successful open. And without the test, you
don't know why they are not opening. You might
print file names after the file test to see if the
script gets there.

 
: #!/usr/local/bin/perl -w -I/apps1/local/lib/perl5/site_perl/5.8.3

    Always use strict. Every single time.

    use strict;

 
: use File::Find;
: $TOP_DIR="../tftpboot";
: $query = "module 3";

    With strict every variable must be declared using 'our'
or 'my'. Always constrain variable scope to the smallest
block of code possible. These variables don't need to be
file scoped. In fact, they don't need to be variables at
all.


: find(sub
: {
: return if -d;

    Here you /could/ use indentation to add clarity:

use File::Find;

find(
    sub {
        return if -d;
        open FILE, "$File::Find::name"
            or die qq(Cannot open "$File::Find::name": $!);

            while ( my $line = <FILE> ) {
                print "$.: $line" if $line =~ /module 3/;
            }

        close FILE;
    }, '../tftpboot' );


    That's easier to read.


: open(FILE,"$File::Find::name");

    This is likely your problem. Always check the return
status of input/output statements. ALWAYS!

 open FILE, "$File::Find::name"
     or die qq(Cannot open "$File::Find::name": $!);


: while(<FILE>) {
: my $line = $_;

    Same as:

 while ( my $line = <FILE> ) {


: chomp($line);

    Why chomp the line if we are going to print it
with the newline added back in?

: $count = $count=1;

    Perl tracks line counts in the $. variable. $count is
not needed.

: next unless ($line =~ /$query/);
: $string.= "line $count: $line\n";
:         }
: if($string ne ''){
: print "$string\n" }
: $count = 0;

    Why beat around the bush. If $string contains anything
then $line =~ /$query/ matched. Stop building $string and
just do the print already. :)

    while ( my $line = <FILE> ) {
        print "$.: $line" if $line =~ /module 3/;
    }


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to