Not sure I'd bracket 'works as expected' and Perl together :-)

For completeness, it's being parsed as:

  print STDERR("+++STDERR: '", ....) ; -- predeclared sub STDERR without a prototype.

  print STDERR() "+++STDERR: '", .... ; -- predeclared sub STDERR ()

where the second is treated as a broken expression involving the return value of STDERR().

This behaviour is consistent with the general handling of subroutines referenced by a bareword.

Where a FILEHANDLE may appear the parser has to jump through hoops to allow print $FH ...., which it parses as a FILEHANDLE and leaves as a run-time problem. With an ordinary print FH .... the parser also leaves checking that FH actually refers to something to run-time.

Having predeclared sub FH () { *FH } ; I can see that the parser is in a bit of a bind when it sees print FH "Hi!";. The prototype says that FH("Hi!") is not the correct interpretation. But it does know that FH is a sub..., even though, on the face of it, it looks just like a filehandle !

I imagine that generalising print FH .... even further, so that the FH could be an expression that returned a filehandle object, would be a nightmare -- leaving broken code to be found at run-time. However, the special case of a predeclared subroutine with a () prototype might be a small step ?

The other thing that is special about filehandles is that there is no way to declare them. Suppose one could declare our FH : filehandle ; (say). Now: (a) strict could throw out print SPELING_ERROR ...; and (b) use of FH in an expression could return *FH{IO} implicitly, so some_sub(FH, ....) or $object->{handle} = FH would work just fine ! Of course, STDERR et al could then be predeclared with a 'filehandle' attribute :-)


In reply to Re^2: sub STDERR { *STDERR } -- nearly works ! by gone2015
in thread 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.