Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: why is it considered good syntax to put \* before STDIN or STDOUT

by davido (Cardinal)
on Jun 08, 2014 at 16:39 UTC ( [id://1089197]=note: print w/replies, xml ) Need Help??


in reply to why is it considered good syntax to put \* before STDIN or STDOUT

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

Replies are listed 'Best First'.
Re^2: why is it considered good syntax to put \* before STDIN or STDOUT
by LanX (Saint) on Jun 08, 2014 at 17:06 UTC
    as a side note, passing a glob also works!
    > perl use strict; use warnings; use diagnostics; sub myprint { my( $fh, $text ) = @_; print $fh $text; } sub STDOUT { print "Woops!\n"; } myprint( *STDOUT, "Hello world!\n" ); __END__ Hello world! >

    I'd rather prefer to be able to disable/deprecate indirect object syntax ...

    ...but unfortunately this doesnt work :(

    > perl my $x=\*STDOUT; $x->print("hu hu"); __END__ Can't locate object method "print" via package "IO::Handle" at - line +2.
    nor this
    > perl open my $x, ">", "/tmp/tst"; $x->print("hu hu"); print `cat /tmp/tst`; __END__ Can't locate object method "print" via package "IO::Handle" at - line +2.

    Cheers Rolf

    (addicted to the Perl Programming Language)

      I'm testing with the Arch Linux system Perl and, unless I'm missing something, it seems that both of your examples that "don't work", work for me.

      $ perl my $x = \*STDOUT; $x->print("STDOUT test"); STDOUT test $ perl open my $x, '>', "/tmp/tst"; $x->print("blah blah"); $ cat /tmp/tst blah blah $ perl -v This is perl 5, version 20, subversion 0 (v5.20.0) built for x86_64-li +nux-thread-multi
        Thanks for testing!

        ...must have been fixed in the meantime! (?)¹

        > perl -v This is perl, v5.10.0 built for i486-linux-gnu-thread-multi

        to which class does $x belong to? still IO::Handle ?

        ... ARGHHHH ...

        Did you explicitly use IO::Handle ?

        My examples work now after importing.

        > perl use IO::Handle; my $x=\*STDOUT; $x->print("hu hu"); __END__ hu hu

        update

        ¹) 5.12.0/perldelta.html#Other-potentially-incompatible-changes

        Filehandles are now always blessed into IO::File .

        The previous behaviour was to bless Filehandles into FileHandle (an empty proxy class) if it was loaded into memory and otherwise to bless them into IO::Handle .

        Cheers Rolf

        (addicted to the Perl Programming Language)

        Did you try these examples with the use strict; and use warnings; pragmas turned on?
Re^2: why is it considered good syntax to put \* before STDIN or STDOUT
by choroba (Cardinal) on Jun 08, 2014 at 16:48 UTC
    Note that turning off strict makes it possible to pass bareword filehandles around as in the first example. Not that I advocate doing either of the two.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1089197]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-20 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found