Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

error on file open

by maynard517 (Initiate)
on Nov 25, 2013 at 22:12 UTC ( #1064320=perlquestion: print w/replies, xml ) Need Help??
maynard517 has asked for the wisdom of the Perl Monks concerning the following question:

I get this error: Permission denied error on open - PS17 at line 22. executing the following code

#*** This script reads the PS17 file and writes it to *** #*** the folder 'archive' and also overwrites 'COMDATA' *** use strict; use warnings; my ($d,$m,$y)=(localtime)[3,4,5]; my $date= sprintf '%02d%02d%02d', $m+1,$d, $y+1900; # my $newame='XM712.XM712.PS00017.', $date; my $threenodes='XM712.XM712.PS00017'; # my DIR = dir("f:\\lawprod\\law\\prd9\\work\\COMDATAFILE\\IN\\"); open_files (); #*** reads the COMDATAFILE/IN directory *** while (my $FILENAME = readdir(DIR) ) { print "filename: $FILENAME\n"; my $comp_name = substr($FILENAME, 0, 19); print "comp_name: $comp_name\n"; print "threenodes: $threenodes\n"; while ($comp_name == $threenodes) { print "dir: DIR\n"; open (PS17, "$FILENAME") or die "$! error on open - PS17"; read_and_write (); }

I am very new to Perl, and don't know how to debug this error. Thank you.

Replies are listed 'Best First'.
Re: error on file open
by GrandFather (Sage) on Nov 25, 2013 at 22:36 UTC

    Change your open (and following) line to:

    open my $PS17, '<', $FILENAME or die "$! error on open - $FILENAME"; read_and_write ($PS17);

    That doesn't fix the problem, but it gives better diagnostics (you know what was being opened), avoids potential problems with some file names by making the open mode explicit, and better manages the lifetime of the file handle. You'll need to update read_and_write to take the file handle as a parameter.

    True laziness is hard work
Re: error on file open
by choroba (Bishop) on Nov 25, 2013 at 22:19 UTC
    Try printing out $FILENAME. Note that == compares numerically, so strings not starting with numbers are usually equal (0 == 0).

    Also, note that readdir returns both files and directories, and that it returns only their names, not full paths.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: error on file open
by Anonymous Monk on Nov 25, 2013 at 22:41 UTC
Re: error on file open
by wazat (Scribe) on Nov 26, 2013 at 05:47 UTC

    Others have addresed the main issues in your program., but I thought I would point out that the first file returned is likely "." followed by ".."

    Also, I don't understand why you base your loop on a comparison of $comp_name and $threenodes. These do not appear to change within the while loop. If it didn't die, it looks like an infinite loop.

Re: error on file open
by hexcoder (Chaplain) on Nov 26, 2013 at 15:28 UTC
    One other point, I have not seen mentioned yet:

    open $PS17, $FILENAME or open my $PS17, '<', $FILENAME open the file only for read access. If you really intend to read_and_write() the opened file, you need to say something like open my $FS17, '+<', $FILENAME or die "$! error on open $FILENAME\n"

    Hope that helps!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1064320]
Approved by GrandFather
Front-paged by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2018-05-23 17:31 GMT
Find Nodes?
    Voting Booth?