Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by LanX (Archbishop)
on Jun 08, 2014 at 17:06 UTC ( #1089199=note: print w/replies, xml ) Need Help??


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

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)

Replies are listed 'Best First'.
Re^3: why is it considered good syntax to put \* before STDIN or STDOUT
by Anonymous Monk on Jun 08, 2014 at 20:32 UTC

    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?

        I just tested with 5.16.2 on OSX and it worked fine under strictures and warnings.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2020-02-24 10:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (104 votes). Check out past polls.

    Notices?