
close error when open is a pipe with no output (and a -w question)
Damn, coming up with good subject lines is tough.
Here is the problem, if I open a filehandle that could potentially
have no output, the close returns false and my or die gets incorrectly
(or at least undesireably called).
Here are the details:
node113% perl -v
This is perl, version 5.003 with EMBED
built under sunos
+ suidperl security patch
(I get the same result for the same version under Solaris 2.5.1)
Here is code that exhibits the problem:
#!/usr/local/bin/perl
open(TEMP,"grep asdf data |") or die "Open(asdf) error:$!\n";
while(<TEMP>) {
chomp; #some meaningless activity
Quote:
}
close(TEMP) or die "Close(asdf) error:$!\n";
open(TEMP,"grep bob data |") or die "Open(bob) error:$!\n";
while(<TEMP>) {
chomp; #some meaningless activity
Quote:
}
close(TEMP) or die "Close(bob) error:$!\n";
And the "data" file is:
node113% cat data
asdf
And finally the error message:
node113% perl -w seek.pl
Close(bob) error:No such file or directory
It is not intuititively obvious to me why this should error. Additionally,
I have other programs that have the same sort of thing that fail with
different error codes. The programs work fine if I remove the 'or die'
but that seems to be a poor solution.
(The other program does an:
open(TEMP,"ypcat something | grep something |") or die "$!\n";
close(TEMP) or die "$!\n";
And the error is: Illegal seek.
)
Oh, and I have another question (sorry didn't want to make the subject
line too long).
I hate the Uninitialized variables message I get with -w. I've read the
thread that came through here a few weeks ago, and the gist seemed to be
to predeclare the variable. The problem I have is with an associative array.
If I want to build this array and I don't know the elements ahead of time, how
do I get around this?
i.e. Let's build the array from the following data:
asdf
bob
fred
jane
asdf
#!/usr/local/bin/perl
%array = '';
open(TEMP,'data') or die "$!\n";
while(<TEMP>) {
chomp;
$array{$_} +=1;
Quote:
}
close(TEMP) or die "$!\n";
When I do a perl -w I get:
node113% perl -w test.pl
Use of uninitialized value at test.pl line 10, <TEMP> chunk 1.
Use of uninitialized value at test.pl line 10, <TEMP> chunk 2.
Use of uninitialized value at test.pl line 10, <TEMP> chunk 3.
Use of uninitialized value at test.pl line 10, <TEMP> chunk 4.
What I have been doing is running my program with -w until I get rid of
everything but these errors and then stop running with -w, but
that too seems unnecessary.
I read the newsgroup, so feel free to simply post the reply.
(If you have other ideas and don't want to post, you can email them
and I'll summarize if people want).
--
Unix System Administrator, Medtronic Micro-Rel
"Subtlety is the art of saying what you think and getting out of the way
before it is understood."