Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

why is it considered good syntax to put \* before STDIN or STDOUT
  • Comment on why is it considered good syntax to put \* before STDIN or STDOUT

Replies are listed 'Best First'.
Re: why is it considered good syntax to put \* before STDIN or STDOUT
by LanX (Saint) on Jun 08, 2014 at 04:00 UTC
    I can only speculate what you are meaning, cause you neither cite sources nor do you show code.

    DB<102> open $x, ">", "/tmp/tst"; DB<103> $x => \*main::$x DB<104> STDIN => "STDIN" DB<105> \*STDIN => \*main::STDIN

    As you can see do lexical file handles actually hold references to globs, i.e. '\*'.

    I never heard that this is generally "considered good syntax", but if you need to store "normal" file handles in variables you'll need this construction.

     my $x = \*STDIN

    see also perldata#Typeglobs-and-Filehandles

    Cheers Rolf

    (addicted to the Perl Programming Language)

      thx
Re: why is it considered good syntax to put \* before STDIN or STDOUT
by davido (Cardinal) on Jun 08, 2014 at 16:39 UTC

    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

      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
      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.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ