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
although the documentation says something else.Can't locate object method "print" via package "IO::Handle"
In reply to Re^2: prototypes for dual-file handle print?
by Anonymous Monk
in thread prototypes for dual-file handle print?
by iaw4
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |