Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

perl CGI: No such file or directory yet file exists???

by jasondancks (Initiate)
on Jan 17, 2015 at 19:27 UTC ( [id://1113610]=perlquestion: print w/replies, xml ) Need Help??

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

To try to eliminate potential issues, the following was ran from the command line:

test script:
#!/usr/bin/perl use CGI qw(:standard); use CGI qw(:standard Vars); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use File::Basename; use lib dirname(__FILE__); use CreateExam; my $cgi = CGI->new(\*STDIN); print $cgi->header(); print $cgi->start_html("test"); my $check = CreateExam->new("/var/www/roger/homeworks/hw13/exam4.txt", +"/var/www/roger/homeworks/hw13/answers4.txt","/var/www/roger/homework +s/hw13/grades4.txt",0); my %ans; $ans{"1"} = "0AD"; $ans{"2"} = "Johnny Manziel"; $ans{"3"} = "washington"; $ans{"4"} = "Shes so hiiiigggghhhhhh\nHigh above me\nShes so hiiiigggg +hhhhhh above me\nCleopatra Joan of Arc Aphrooooodiiiiitttteeee\n"; print $check->entergrades("test",\%ans); print qq(<div style="margin:10px;border:solid;border-width:10px;border +-color:#FFFFFF">\n); $check->printgrades("test"); print "</div>\n"; print $cgi->end_html();
CreateExam.pm:
sub new { my ($class,$file,$answers,$grades,$script) = @_; #print "<p>in new: file: $file, grades: $grades</p>\n"; return bless {'file'=>$file,'answers'=>$answers,'gradefile'=>$grad +es,'script'=>$script},$class; } .... sub entergrades { my ($self,$person,$answers) = @_; my $afile = $self->{'answers'}; open(ANSWERS,"<$afile") or die "$afile: $!"; my $grades = $self->{'gradefile'}; open(GRADES,"<<$grades") or die "got error $! (".($!+0).")\n"; # p +roblem right here ... }
output:
Software error: got error No such file or directory (2)
double check file exists:
jddancks@debian-macbook:/var/www/roger/homeworks/hw13$ ls -l /var/www/ +roger/homeworks/hw13/grades4.txt -rwxr-xr-x 1 jddancks jddancks 2095 Jan 16 16:30 /var/www/roger/homewo +rks/hw13/grades4.txt

Replies are listed 'Best First'.
Re: perl CGI: No such file or directory yet file exists???
by fishmonger (Chaplain) on Jan 17, 2015 at 19:51 UTC

    What is the value of $grades

    Add the that var to the die statement to verify it holds what you expect.

    open(GRADES,'<', $grades) or die "failed to open '$grades' $! (".($!+0).")\n";
Re: perl CGI: No such file or directory yet file exists???
by graff (Chancellor) on Jan 17, 2015 at 20:38 UTC
    In addition to fishmonger's advice above, you should consider adding:
    use Cwd;
    so that you can include the process's current working directory in the error message:
    open( GRADES, '<', $grades ) or die sprintf("error opening %s: %s (%d) cwd=%s\n", $grades, $!, $ +!+0, getcwd );
Re: perl CGI: No such file or directory yet file exists???
by NetWallah (Canon) on Jan 18, 2015 at 01:38 UTC
    Your OPEN statement :
    open(GRADES,"<<$grades") ...
    is incorrect - it is taking the second "<" as a part of the file name.

    Follow graff's advice above., or (better), consider using local file handles in addition to the 3-arg open, and avoiding single-use throwaway variables:

    open (my $grades, "<", $self->{gradefile}) or die ....
    I might have gone further to suggest what I usually do - save the file handle as a part of the object, like:
    open ( $self->{gradefh}, "<", $self->{gradefile})
    but this makes it tricky to read/write the file - you need extra braces.

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1113610]
Approved by graff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found