H
Hendrik Maryns
Hi,
I've got this little program that reads all the files ending on .pos in
a directory into a list @dir, then opens them one by one, does
something, closes them again. When finished with all files, I start
this same thing for a second time, doing something different with the
files, based on what I did with them in the first loop. Now strangely
enough I get an error message which says there are uninitialised
variables in @dir, "Can't open etc."
I get this (with diagnostics):
Use of uninitialized value in open at test.pl li
ne 21 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a
mistake.
To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl tells you what
operation
you used the undefined value in. Note, however, that perl
optimizes your
program and the operation displayed in the warning may not necessarily
appear literally in your program. For example, "that $foo" is
usually optimized into "that " . $foo, and the warning will refer to
the concatenation (.) operator, even though there is no . in your
program.
Uncaught exception from user code:
No such file or directory at test.pl line 21.
at test.pl line 21
If I redefine the @dir variable, it works, but this shouldn't be
necessary if you ask me...
Though of course you can't test this because you don't have those files
in your active directory, here's a reduced version:
use strict;
use warnings;
use diagnostics;
my @dir= <*.pos>; # feel free to change this for testing purposes
for (@dir){
open(my $in, '<', $_)||die($!);
# do something
close $in;
} #endfor
# @dir= <*.pos>;
for (@dir){
open(my $in, '<', $_ )||die($!);
#do something else
close $in;
} #endfor
So my question is: how come? Where does @dir get altered, or how come
$_ is undefined there.
For "do something" you could for example use
my $counter=0;
while (<$in>){$counter++;
print $counter, ' ';
Curious greetings, Hendrik
I've got this little program that reads all the files ending on .pos in
a directory into a list @dir, then opens them one by one, does
something, closes them again. When finished with all files, I start
this same thing for a second time, doing something different with the
files, based on what I did with them in the first loop. Now strangely
enough I get an error message which says there are uninitialised
variables in @dir, "Can't open etc."
I get this (with diagnostics):
Use of uninitialized value in open at test.pl li
ne 21 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a
mistake.
To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl tells you what
operation
you used the undefined value in. Note, however, that perl
optimizes your
program and the operation displayed in the warning may not necessarily
appear literally in your program. For example, "that $foo" is
usually optimized into "that " . $foo, and the warning will refer to
the concatenation (.) operator, even though there is no . in your
program.
Uncaught exception from user code:
No such file or directory at test.pl line 21.
at test.pl line 21
If I redefine the @dir variable, it works, but this shouldn't be
necessary if you ask me...
Though of course you can't test this because you don't have those files
in your active directory, here's a reduced version:
use strict;
use warnings;
use diagnostics;
my @dir= <*.pos>; # feel free to change this for testing purposes
for (@dir){
open(my $in, '<', $_)||die($!);
# do something
close $in;
} #endfor
# @dir= <*.pos>;
for (@dir){
open(my $in, '<', $_ )||die($!);
#do something else
close $in;
} #endfor
So my question is: how come? Where does @dir get altered, or how come
$_ is undefined there.
For "do something" you could for example use
my $counter=0;
while (<$in>){$counter++;
print $counter, ' ';
Curious greetings, Hendrik