http://www.perlmonks.org?node_id=1012494


in reply to Open a directory and recursively process files

Dear Monk,
Did I or did I not just answer this question here? Why are you reposting this with a different subject?


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
  • Comment on Re: Open a directory and recursively process files

Replies are listed 'Best First'.
Re^2: Open a directory and recursively process files
by Dr Manhattan (Beadle) on Jan 09, 2013 at 20:06 UTC

    Hi blue cowdawg

    I tried your sub, but it still does not work. ".:Permission denied at project6.pl line 16." Which is    open FIN,"< $fname" or die "$fname:$!"; Any idea why this happens?

    #!/usr/bin/perl -w use strict; my $dir = 'C:\Users\Zandre Botha\Desktop\Text Files'; opendir (DIR, $dir) or die "cannot opendir $dir"; foreach my $file (readdir(DIR)) { &process_file ($file); } sub process_file { my $fname=join("/",@_); open FIN,"< $fname" or die "$fname:$!"; my @lines=<FIN>; chomp @lines; close FIN; my $count=0; foreach my $line(@lines) { map { $count++ } split (/[\s\t\n]+/,$line); } printf "There are %d words in %s\n",$count,$fname; } closedir (DIR);
          I tried your sub, but it still does not work. ".:Permission denied at project6.pl line 16." Which is open FIN,"< $fname" or die "$fname:$!"; Any idea why this happens?

      If you read the diagnostic carefully you'll notice it complains about the filename "." which is actually your current working directory. You forgot the line

      next if ($file eq '.') || ( $file eq '..');
      just prior to the call to the sub. I think I mentioned that a few answers ago...


      Peter L. Berghold -- Unix Professional
      Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

      Any idea why this happens?

      Because you don't have permission -- ask the computer why

      stat, file, ls

      or die Fudge( "q{$file}:" ); sub Fudge { use Errno(); join qq/\n/, "Error @_", map { " $_" } int( $! ) . q/ / . $!, int( $^E ) . q/ / . $^E, grep( { $!{$_} } keys %! ), q/ /; }
      You failed to include a couple of lines of code you were given the last time you asked this question. Guess which ones:Re^2: Open a folder