Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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 cp165.pl 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.

Comment on error on file open
Download Code
Re: error on file open
by choroba (Abbot) 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 GrandFather (Cardinal) 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 Anonymous Monk on Nov 25, 2013 at 22:41 UTC
Re: error on file open
by wazat (Beadle) 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 (Hermit) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1064320]
Approved by GrandFather
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-09-03 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (35 votes), past polls