Reputation: 1142
The error occurs in a custom perl module for reading in a file. This uses CGI so the error messages get displayed in the browser.
my $check = CreateExam->new("exam2.txt","grades.txt");
$check->readexam();
CreateExam.pm:
sub new {
my ($class,$file,$grades) = @_;
print "<p>in new: file: $file, grades: $grades</p>\n";
return bless {'file'=>$file,'gradefile'=>$grades},$class;
}
sub readexam {
my $self = shift;
print "<p>in readexam File: $self->{'file'}</p>\n";
if (defined($self->{'file'})) {
my $self->{'questions'} = [];
my $count = -1;
my $file = $self->{'file'};
open(my $handle,"<$file") or die "<p>it was the open. File: $file</p>";
while(<$handle>) {
ouput:
in new: file: exam2.txt, grades: grades.txt
in readexam File: exam2.txt
Use of uninitialized value $file in concatenation (.) or string at /var/www/homeworks/hw10/CreateExam.pm line 35 (#1)
Software error:
<p>it was the open. File: </p> at /var/www/homeworks/hw10/CreateExam.pm line 35.
Software error:
[Sun Dec 21 14:30:31 2014] hw10.cgi: <p>it was the open. File: </p> at /var/www/homeworks/hw10/CreateExam.pm line
[Sun Dec 21 14:30:31 2014] hw10.cgi: [Sun Dec 21 14:30:31 2014] hw10.cgi:
it was the open. File:
at /var/www/homeworks/hw10/CreateExam.pm line 35.
I had use warnings
and use diagnostics
. It seems strange that I get a part of the error message twice.
Upvotes: 0
Views: 1190
Reputation: 67900
You create a new variable $self
at this point in your code:
my $self->{'questions'} = [];
my $count = -1;
my $file = $self->{'file'};
Then you try to access it and assign a value to another variable $file
, which will become undefined. When you use my
you create a new variable in the current scope.
So the answer is probably: Don't use my
in this case. You have already used it once at the top of the sub.
However...
It is completely redundant to assign a new array ref to the key questions
. Perl's autovivification can handle that, e.g. if you do push @{ $self->{questions} }, $foo
, you will create an array ref.
Upvotes: 4