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

I needed to go through a directory finding files without a string in them, easy I thought, but kept getting the error message after 30 files.

Permission denied Found to be obsolete at find.pl line 13.

The file it stops at does not appear to be any different and there are sub directories which I don't need to search

heres the code
foreach my $file (readdir(DIR)){ if($file ne '.' and $file ne '..' and ! (-d $file)){ print "$file\n"; #below is line 13 ithe original program open FILE, $dir.$file or die "$! $file"; foreach my $line (<FILE>){ if ($line =~ /use SBSDBRUN/){ $found = $file; last; } } if (! $found ){ push @files, $file; } undef ($found); close FILE; } }
Thanks anyone for your help.

Replies are listed 'Best First'.
Re: Permission denied Found to be Obsolete
by dorward (Curate) on Sep 26, 2006 at 08:35 UTC

    At a guess, you haven't shown us all the code (where is the shebang line? Where is use strict and use warnings? etc), and that line 13 isn't "last;" but is actually "open FILE, $dir.$file or die "$! $file";", and that you have a file in the directory called "Found to be obsolete" that the user the Perl is running as doesn't have permission to read.

      All correct and I have added the the comment "below is the orginal line 13" However I now have the answer.

      And its in the checking of the directory. It should read
      if($file ne '.' and $file ne '..' and ! (-d $dir.$file)){ ##code }

      A trap I should have seen I admitt but sometimes putting on here clears ones head.

        !(-d $name)
        should be
        -f $name

        After all, there's plenty of things a directory entry can be besides a plain file and a directory.

        You should probably also consider whether the name test shouldn't instead be

        $file !~ /^\./
        because, theoretically, any file whose name begins with dot is supposed to be "hidden". Of course, you may in fact want to search the hidden files as well; I don't know.

        We're building the house of the future together.
Re: Permission denied Found to be Obsolete
by Scarborough (Hermit) on Sep 27, 2006 at 14:08 UTC
    Thanks for all your help on this one and just shows again the TIMTOWTDI ness of PERL. This problem was orginally a a get out of jail quick card and did help me locate what I wanted and come up with a soloution so job done

    But for anyone interested there was a directory called "Found to be Obsolete" and I think this is what clouded my judgement, it just looked like it should be in an error message. Might use
    open FILE, $dir.$file or die "$! <<<<$file>>>>";
    Just so I know its me thats put it there. Also the irony which struck me after banging my head on the wall and spotting my failing test was that I could have used
    if ($file =~ /\.pm/){ #do something }
    I would have achived the same, but its become a habit for me to use the '.' and '..' approach. We live and learn.

    Thanks for the help everyone

Re: Permission denied Found to be Obsolete
by Anonymous Monk on Sep 26, 2006 at 16:26 UTC
    You don't happen to have a file literally named "Found to be obsolete", do you? For a similar situation (see below), you can see that the $! variable prints out Permission denied. The die function, since it's called on a string that doesn't end in \n, appends at find.pl line 13. That leaves the rest of the error message (Found to be obsolete) as the unknown -- $file is the only thing that can account for it. Simple test case:
    $ touch foo $ chmod 000 foo $ perl -e '$file = "foo"; open FILE, $file or die "$! $file";' Permission denied foo at -e line 1.