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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.