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

Dear monks, i have a task to open a directory and read thru some 100 or 150 text files in it line by line and substitute a value for each .when i read thru half way of the 150 txt files i get a permission denied at xx.pl line xx error and the script terminates ubruptly. i even tried using flock,but no use. Please help me on this? Thanks, seeker this is my code
use File::Basename; use Fcntl qw(:flock); print"enter the input directory path:\n"; chomp($indir=<STDIN>); print"enter the events directory path:\n"; chomp($evdir=<STDIN>); print"enter the output directory name:\n"; chomp($tmpdir=<>); if (($indir eq $outdir)||($tmpdir eq $outdir)||($indir eq $tmpdir)) { print"you cannot have same input and ouput directory please change:\n" +; exit(); } else { $ever="$evdir/events.txt"; chdir ("$indir") or die "$!"; opendir(DIR,".") or die "$!"; my @files=readdir DIR; close DIR; my %result=(); foreach $file(@files) { unless (($file eq ".") || ($file eq "..") ) { $base=basename("$file",".txt"); $filein="$indir/$file"; $srtfile="$outdir/$base._events.txt"; $tmpout="$tmpdir/$base._unsortedevents.txt"; open evein,$ever or die $!; flock(evein, LOCK_EX) or die "Cannot lock - $!"; while (<evein>) { chomp; next unless length; my ($key,$value)=split /===/,$_; $result{$key}=$value; } close(evein); while (my($key,$value)=each (%result)) { open fln,$filein or die $!; flock(fln, LOCK_EX) or die "Cannot lock - $!"; while(<fln>) { my ($line)=$_; chomp($line); if ($line=~m/$key/ig) { $line=~s/$key/$value/eeig; open flow,">>$tmpout" or die $!; #prints matching log lines in events. flock(flow,LOCK_EX) or die "Cannot lock - $!"; print flow $line,"\n"; } } print $value,"\n"; close(flow); } } } print"<----------------------------------------------->\n"; print "\t\t action done\n"; print"<----------------------------------------------->\n"; print "\t\t$count results found in search\n"; print"<****************..............******************>>\n"; close $out; }
  • Comment on permission denied error when reading thru set of text files at once!!!
  • Download Code

Replies are listed 'Best First'.
Re: permission denied error when reading thru set of text files at once!!!
by jwkrahn (Abbot) on Mar 15, 2011 at 06:42 UTC
    print"enter the input directory path:\n"; chomp($indir=<STDIN>); print"enter the events directory path:\n"; chomp($evdir=<STDIN>); print"enter the output directory name:\n"; chomp($tmpdir=<>);

    Why do you use <STDIN> for the first two variables and <> for the third?



    opendir(DIR,".") or die "$!"; my @files=readdir DIR; close DIR;

    You have to use closedir to close a directory handle opened with opendir.



    open evein,$ever or die $!; flock(evein, LOCK_EX) or die "Cannot lock - $!"; while (<evein>) { chomp; next unless length; my ($key,$value)=split /===/,$_; $result{$key}=$value; } close(evein);

    The value of $ever doesn't change inside this loop so this code should be outside the loop where $ever is set.



    open evein,$ever or die $!;

    You are trying to open 'events.txt' from a directory entered by the user.    If the user uses a relative path then your program won't be able to find it.    You should give the user more information than just the contents of $!.



    flock(evein, LOCK_EX) or die "Cannot lock - $!";

    You can't LOCK_EX a file opened only for input, only for output.



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

    That is usually written as:

    while ( my $line = <fln> ) {

      You can't LOCK_EX a file opened only for input, only for output.

      $ perl -E' use Fcntl qw( LOCK_EX ); open(my $fh, "<", ".bashrc") or die; flock($fh, LOCK_EX) or die; say "ok"; ' ok

      Can't? no. But does it make sense to? Probably never.

        flock is implemented several different ways in different builds of Perl. Some of those ways indeed do not even allow exclusive locks against read-only file handles.

        - tye        

Re: permission denied error when reading thru set of text files at once!!!
by GrandFather (Saint) on Mar 15, 2011 at 04:59 UTC

    Just use strictures (use strict; use warnings;) and you will see that there is an error on line xx - 3 that is causing the problem.

    If that doesn't help (although it should, strictures always help) you may have to actually show us the code.

    Update: Oh, I see you made a stealth update to your node. Still, you haven't told us which line the error occurs on

    Update: on downloading your code and adding strictures I notice that $outdir is never initialised but is tested against.

    Also flow is used as a file handle for multiple files inside a while loop and the files only ever have one line written to them. flow is only closed once (regardless of whether it was opened or not) outside the while loop.

    $count is used in a print, but is never initialised.

    File handle out is closed but it is never opened or otherwise used.

    True laziness is hard work
    A reply falls below the community's threshold of quality. You may see it by logging in.