hector89 has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl use warnings; use strict; use File::Find; find(\&file_names, $ARGV[0]); sub file_names { if(-f $File::Find::name=~/.txt$/) { print "$File::Find::name\n"; my @files=$File::Find::name; } }
i have written this code for looking .txt file through directories and subdirectories inside that passed directory but it's not working..anyone suggest why>?
  • Comment on code for serching .txt file through directories and sub directories
  • Download Code

Replies are listed 'Best First'.
Re: code for serching .txt file through directories and sub directories
by zentara (Cardinal) on Jun 12, 2012 at 08:50 UTC
    Look at this code and check for differences. Your -f test was being regexed, not the filename. Also you filled your array improperly.
    #!/usr/bin/perl use warnings; use strict; use File::Find; find(\&file_names, '.'); my @files; print "@files\n"; sub file_names { # if( -f && $File::Find::name=~/(.*).txt$/) #bad usage of .* if( -f && $File::Find::name=~/\.txt$/) # thanks davido :-) { print "$File::Find::name\n"; push @files, $File::Find::name; } }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: code for serching .txt file through directories and sub directories
by davido (Cardinal) on Jun 12, 2012 at 11:40 UTC

    Another issue that hasn't been mentioned: dot. You want to match a literal '.', not an "anything except for newline" character, right? /\.txt$/

    This isn't your only issue. But the other issues have already been addressed satisfactorily. I just can't stand looking at the unescaped metacharacter when I know you intended it to be a literal. :)


    Dave

Re: code for serching .txt file through directories and sub directories
by marto (Cardinal) on Jun 12, 2012 at 08:42 UTC

      "Consider initializing @files outside of your if condition".

      Indeed. And pushing to it rather than assigning to it.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: code for serching .txt file through directories and sub directories
by Khen1950fx (Canon) on Jun 12, 2012 at 14:46 UTC
    File::Find was cool ten years ago, but I've gotten older and lazier; instead, I use File::Find::Rule most of the time. This does what you want:
    #!/usr/bin/perl -l use strict; use warnings; use File::Find::Rule; my $dir = '/tmp'; my @subdirs = File::Find::Rule->in($dir); my $rule = File::Find::Rule->new; $rule->file; $rule->name('*.txt'); my @files = $rule->in(@subdirs); foreach my $file(@files) { print $file; }

      It really doesn't look like you've used it much

      my @files = File::Find::Rule->file->name('*.txt')->in( $dir );
Re: code for serching .txt file through directories and sub directories
by GrandFather (Saint) on Jun 12, 2012 at 08:43 UTC

    What do you expect -f $File::Find::name=~/.txt$/ to do?

    True laziness is hard work
Re: code for serching .txt file through directories and sub directories
by Anonymous Monk on Jun 12, 2012 at 08:43 UTC

    i have written this code for looking .txt file through directories and subdirectories inside that passed directory but it's not working..anyone suggest why?

    you're giving it a non-existent directory

    you're giving it a directory to which your program/login doesn't have permission to read

    you're giving it a directory which has no ".txt" files

    you're not running that program but some other program -- use full path to your program

    Add more print statements, use more Basic debugging checklist