in reply to Re: prototypes for dual-file handle print?
in thread prototypes for dual-file handle print?

There's no way to define a sub to have the same syntax as print

I always thought function $foo $bar is another notation for $foo->function($bar) (which is also often used to imitate the new operator from other languages). So for example the following works:

use warnings; use strict; my $mp = MyPrint->new(*STDERR); $mp->myprint("foobar\n"); myprint $mp "foobar\n"; package MyPrint; sub new { my ($class, $out) = @_; return bless { out => $out }, $class; } sub myprint { my ($self, $text) = @_; my $out = $self->{out}; print $out $text; }

I don't really know why STDERR->print "foobar" doesn't work though. It dies with

Can't locate object method "print" via package "IO::Handle"
although the documentation says something else.

Replies are listed 'Best First'.
Re^3: prototypes for dual-file handle print?
by ikegami (Patriarch) on Jul 06, 2010 at 16:23 UTC

    I always thought function $foo $bar is another notation for $foo->function($bar)

    Ah yes. Indirect method call. It can cause headaches, so I don't use it. If a function or op with the same name as the method exists in the current namespace, it's not parsed as an indirect method call. Since print is an op, print $obj ... will never be an indirect method call, and it can't be used to solve the OP's problem.

Re^3: prototypes for dual-file handle print?
by Fletch (Bishop) on Jul 06, 2010 at 15:07 UTC
    $ perl -le 'use IO::Handle();STDOUT->print( qq{A "use IO::Handle" help +s immensely} );' A "use IO::Handle" helps immensely

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.