So, I thought I had a brainwave....
I generally don't bother with FH type filehandles, but open $FH, ...., and use $FH instead.
Problem is handling STDERR etc. the same way.
Simple approach is to simply do:
$STDERR = *STDERR{IO} ;
at some early stage, and use $STDERR thereafter. I do this in a module along with my other favourite bits and pieces, and export $STDERR et al.
I just read that exporting variables is a Bad Thing. So I tried another approach. I tried creating a STDERR subroutine, which returns *STDERR. Stripping down to the basics, and adding a little test stuff, this looks something like:
use strict ; use warnings ; sub STDERR { *STDERR } ; p(STDERR, "---Hello world: '", STDERR, "'\n") ; print STDERR "+++STDERR: '", STDERR, "'\n" ; sub p { my ($H, @p) = @_ ; print $H "//[$H] ", @p ; } ;
and the result is, STDERR:
//[*main::STDERR] ---Hello world: '*main::STDERR'
and, STDOUT:
*main::STDERR
Which indicates that STDERR() is doing what was hoped for. So far so good. The sad part is that STDERR is no longer working as a filehandle ! At least as far as print is concerned.
What's really odd is that the parser appears to have accepted the print STDERR as a filehandle -- for if not, there would have been a lot of compile time complaints about possible missing operator ! However, something clearly goes wrong -- and there are no errors or warnings raised. Quite independently of whether this trick "should" work, the current behaviour looks like a failure of diagnostics at least.
FWIW, it occurred to me that sub STDERR ought to look like a constant, thus:
use strict ; use warnings ; sub STDERR () { *STDERR } ; p(STDERR, "---Hello world: '", STDERR, "'\n") ; print STDERR "+++STDERR: '", STDERR, "'\n" ; sub p { my ($H, @p) = @_ ; print $H "//[$H] ", @p ; } ;
but Perl throws up all over that, as follows:
String found where operator expected at tst.pl line 8, near "STDERR +"+++STDERR: '"" (Do you need to predeclare STDERR?) syntax error at tst.pl line 8, near "STDERR "+++STDERR: '"" Execution of tst.pl aborted due to compilation errors.
...which seems to indicate that I'm upsetting something !
Wadya think ?
Chris
In reply to sub STDERR { *STDERR } -- nearly works ! by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |