in reply to permission denied error when reading thru set of text files at once!!!

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> ) {

Replies are listed 'Best First'.
Re^2: permission denied error when reading thru set of text files at once!!!
by ikegami (Patriarch) on Mar 15, 2011 at 06:47 UTC

    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