STDOUT is a "bareword filehandle". Bare-words are discouraged because they introduce a potential source for bugs. Watch this:

use strict; use diagnostics; sub myprint { my( $fh, $text ) = @_; print $fh $text; } myprint( STDOUT, "Hello world!\n" );

This will produce the following:

Bareword "STDOUT" not allowed while "strict subs" in use at mytest.pl +line 15. Execution of mytest.pl aborted due to compilation errors (#1) (F) With "strict subs" in use, a bareword is only allowed as a subroutine identifier, in curly brackets or to the left of the "=> +" symbol. Perhaps you need to predeclare a subroutine? Uncaught exception from user code: Bareword "STDOUT" not allowed while "strict subs" in use at mytest +.pl line 15. Execution of mytest.pl aborted due to compilation errors.

...which is a pretty good explanation; barewords introduce an ambiguity. Watch this:

use strict; use diagnostics; sub myprint { my( $fh, $text ) = @_; print $fh $text; } sub STDOUT { print "Woops!\n"; } myprint( STDOUT, "Hello world!\n" );

...which produces...

Woops! Can't use string ("1") as a symbol ref while "strict refs" in use at m +ytest.pl line 8 (#1) (F) You've told Perl to dereference a string, something which use strict blocks to prevent it happening accidentally. See "Symbolic references" in perlref. This can be triggered by an @ o +r $ in a double-quoted string immediately before interpolating a varia +ble, for example in "user @$twitter_id", which says to treat the conten +ts of $twitter_id as an array reference; use a \ to have a literal @ symbol followed by the contents of $twitter_id: "user \@$twitter_i +d". Uncaught exception from user code: Can't use string ("1") as a symbol ref while "strict refs" in use +at mytest.pl line 8. main::myprint(1, 'Hello world!\x{a}') called at mytest.pl line 15

What's going on is a little nuanced. First a subroutine named STDOUT is executed, printing "Woops!\n". The return value is the return value of print, which is '1' if it was successful. It was successful, because it successfully printed "Woops!\n". That '1' is now treated as a symbolic reference, which is obviously not what you want.

Now let's clean things up:

use strict; use diagnostics; sub myprint { my( $fh, $text ) = @_; print $fh $text; } sub STDOUT { print "Woops!\n"; } myprint( \*STDOUT, "Hello world!\n" );

This prints "Hello world!", which is probably what we wanted all along. In this case, we're explicitly passing a reference to the typeglob *STDOUT, which is a filehandle. It doesn't matter that there's a subroutine named &STDOUT, because &STDOUT is a different entity from *STDOUT, and our chosen syntax disambiguates this for Perl.


Dave


In reply to Re: why is it considered good syntax to put \* before STDIN or STDOUT by davido
in thread why is it considered good syntax to put \* before STDIN or STDOUT 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.