Closed p5pRT closed 20 years ago
folks\,
perlbug.rep attached. In case it doesnt just inline for you\, heres the jist again:
GIVEN:
sub open2scalar { my $msg = shift; open ($GH\, '>'\, \my $tmp) or warn $!; print $GH "hello from open2scalar: $msg\n"; print "at line# "\, __LINE__\, ": $tmp"; } open2scalar 'b4 handle init'; $GH = 'foo'; open2scalar 'handle set to string'; $GH = \*STDOUT; open2scalar 'handle set to \STDOUT';
both bleadperl and 5.8.3 yield following:
[jimc@harpo bleadperl]$ ./perl -w ~/perl-play/open-bug-test
at line# 44: hello from open2scalar: b4 handle init at line# 44: hello from open2scalar: handle set to string Bad file descriptor at /home/jimc/perl-play/open-bug-test line 42. hello from open2scalar: handle set to \STDOUT Use of uninitialized value in concatenation (.) or string at /home/jimc/perl-play/open-bug-test line 44. at line# 44: [jimc@harpo bleadperl]$
Note that although the 3rd call also prints\, it is missing the 'at line# 44' part. This is cuz the open failed\, and $GH is still STDOUT. The line#44 prints afterwards\, and after the error about $tmp being uninitialized.
The warning is less than ideal\, but since $! is constrained to match OS errors\, this is probably a DOC-BUG.
I assume this doc-patch belongs in 'perldoc -f open' output\, but I cant find that file ;-)
GIVEN:
sub open2scalar { my $msg = shift; open ($GH\, '>'\, \my $tmp) or warn $!; print $GH "hello from open2scalar: $msg\n"; print "at line# "\, __LINE__\, ": $tmp"; } open2scalar 'b4 handle init'; $GH = 'foo'; open2scalar 'handle set to string'; $GH = \*STDOUT; open2scalar 'handle set to \STDOUT';
both bleadperl and 5.8.3 yield following:
[jimc@harpo bleadperl]$ ./perl -w ~/perl-play/open-bug-test
at line# 44: hello from open2scalar: b4 handle init at line# 44: hello from open2scalar: handle set to string Bad file descriptor at /home/jimc/perl-play/open-bug-test line 42. hello from open2scalar: handle set to \STDOUT Use of uninitialized value in concatenation (.) or string at /home/jimc/perl-play/open-bug-test line 44. at line# 44: [jimc@harpo bleadperl]$
Note that although the 3rd call also prints\, it is missing the 'at line# 44' part. This is cuz the open failed\, and $GH is still STDOUT. The line#44 prints afterwards\, and after the error about $tmp being uninitialized.
The warning is less than ideal\, but since $! is constrained to match OS errors\, this is probably a DOC-BUG.
I assume this doc-patch belongs in 'perldoc -f open' output\, but I cant find that file ;-)
Jim Cromie (via RT) wrote:
# New Ticket Created by Jim Cromie # Please include the string: [perl #27714] # in the subject line of all future correspondence about this issue. # \<URL: http://rt.perl.org:80/rt3/Ticket/Display.html?id=27714 >
folks\,
perlbug.rep attached. In case it doesnt just inline for you\, heres the jist again:
GIVEN:
sub open2scalar { my $msg = shift; open ($GH\, '>'\, \my $tmp) or warn $!; print $GH "hello from open2scalar: $msg\n"; print "at line# "\, __LINE__\, ": $tmp"; } open2scalar 'b4 handle init'; $GH = 'foo'; open2scalar 'handle set to string'; $GH = \*STDOUT; open2scalar 'handle set to \STDOUT';
both bleadperl and 5.8.3 yield following:
[jimc@harpo bleadperl]$ ./perl -w ~/perl-play/open-bug-test
at line# 44: hello from open2scalar: b4 handle init at line# 44: hello from open2scalar: handle set to string Bad file descriptor at /home/jimc/perl-play/open-bug-test line 42. hello from open2scalar: handle set to \STDOUT Use of uninitialized value in concatenation (.) or string at /home/jimc/perl-play/open-bug-test line 44. at line# 44: [jimc@harpo bleadperl]$
Note that although the 3rd call also prints\, it is missing the 'at line# 44' part. This is cuz the open failed\, and $GH is still STDOUT. The line#44 prints afterwards\, and after the error about $tmp being uninitialized.
The warning is less than ideal\, but since $! is constrained to match OS errors\, this is probably a DOC-BUG.
I assume this doc-patch belongs in 'perldoc -f open' output\, but I cant find that file ;-)
Jim\,
perldoc -f open:
File handles can be opened to "in memory" files held in Perl scalars via:
open($fh\, '>'\, \$variable) || ..
Though if you try to re-open "STDOUT" or "STDERR" as an "in memory" file\, you have to close it first:
close STDOUT; open STDOUT\, '>'\, \$variable or die "Can't open STDOUT: $!";
so your code needs to be:
sub open2scalar { my $msg = shift; open ($GH\, '>'\, \my $tmp) or warn $!; print $GH "hello from open2scalar: $msg\n"; print "at line# "\, __LINE__\, ": $tmp"; } open2scalar 'b4 handle init'; $GH = 'foo'; open2scalar 'handle set to string'; close STDOUT; $GH = \*STDOUT; open2scalar 'handle set to \STDOUT';
or you could arrange for open2scalar to close the fh before it opens it.
__________________________________________________________________ Stas Bekman JAm_pH ------> Just Another mod_perl Hacker http://stason.org/ mod_perl Guide ---> http://perl.apache.org mailto:stas@stason.org http://use.perl.org http://apacheweek.com http://modperlbook.org http://apache.org http://ticketmaster.com
The RT System itself - Status changed from 'new' to 'open'
"Well\, don't do that\, then."
What with the select() operator\, there's no need to reopen STDOUT in magic fashion. (There can be a need to reopen STDOUT in non-magic ways\, for inheritance by child processes. But magic doesn't survive exec()\, so there's no point with that.) -- Chip Salzenberg - a.k.a. - \chip@​pobox\.com "I wanted to play hopscotch with the impenetrable mystery of existence\, but he stepped in a wormhole and had to go in early." // MST3K
Stas Bekman wrote:
perldoc -f open:
thanks Stas - that directly addresses my prob - ie 'dont do that'. I obviously missed that somehow.
NOTABUG.
as to your suggested workaround\, I solved it differently\,
open (my $tmp\, '>'\, \my $buff); $GH = $tmp;
This should implicitly close whatever's already in $GH\, without inadvertently trying to close an undef\, or having to test its current value.
I dont know whether this has any other corner cases\, but I raise it here cuz I extracted this test-case from my patch #24821\, and ICBW there also. (youre welcome to comment there too ;-)
jim cromie wrote:
Stas Bekman wrote:
perldoc -f open:
thanks Stas - that directly addresses my prob - ie 'dont do that'. I obviously missed that somehow.
Besides\, in-memory vars are broken with regards to STD streams. I've reported that earlier\, but didn't submit a proper bug report. I will do that now.
__________________________________________________________________ Stas Bekman JAm_pH ------> Just Another mod_perl Hacker http://stason.org/ mod_perl Guide ---> http://perl.apache.org mailto:stas@stason.org http://use.perl.org http://apacheweek.com http://modperlbook.org http://apache.org http://ticketmaster.com
@rspier - Status changed from 'open' to 'resolved'
Migrated from rt.perl.org#27714 (status was 'resolved')
Searchable as RT27714$