Leser: 23
Can't locate object method "print" via package "IO::Handle" at ./printtest.pl line 5.
1 2 3 4 5 6
#!/usr/bin/perl use strict; use warnings; use IO::Handle; STDOUT->print("TEST\n");
1 2 3 4 5
sub print { @_ or croak 'usage: $io->print(ARGS)'; my $this = shift; print $this @_; }
1 2 3 4 5
#!/usr/bin/perl use strict; use warnings; TEST->print("NOCH EIN TEST\n");
Can't locate object method "print" via package "TEST" (perhaps you forgot to load "TEST"?) at ./printtest.pl line 5.
1
2
3
4
$ perl -lwe 'my $x = bless{}, "CGI"; print $x->param'
Can't locate object method "param" via package "CGI" at -e line 1.
$ perl -lwe 'use CGI; my $x = bless{}, "CGI"; print $x->param'
1 2 3 4 5 6 7 8 9 10 11 12
#!/usr/bin/perl use strict; use warnings; #use IO::Handle; sub IO::Handle::print { my $fh=shift; print $fh @_; } STDOUT->print("NOCH EIN TEST\n");
1
2
3
$ perl -lwe 'open F, "/dev/null" or die; print *F{IO}; print *STDOUT{IO}'
IO::Handle=IO(0x926e508)
IO::Handle=IO(0x9256610)
1
2
3
4
5
$ perl -lwe 'open my $f, "/dev/null" or die; $f->print'
Can't locate object method "print" via package "IO::Handle" at -e line 1.
$ perl -MIO::Handle -lwe 'open my $f, "/dev/null" or die; $f->print'
Filehandle $f opened only for input at /usr/lib/perl/5.10/IO/Handle.pm line 148.
2010-02-04T20:56:15 betterworldWenn bei normalen Objekten "$obj->meth" dasselbe ist wie "meth $obj" (Stichwort indirect object syntax), wird "F->print" offenbar anders geparst als "print F", wobei das erstere ein gewoehnlicher Methodenaufruf (wie bei $obj) ist, das zweitere der print-Operator. Beides funktioniert nur, wenn es das Dateihandle F gibt, wobei afaik ein bisschen Heuristik benutzt wird.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#!/usr/bin/perl use strict; use warnings; package MyPrint; sub new { my $class=shift; my $file=shift; open(my $FH,'>',$file); return bless(\*$FH,ref($class)?ref($class):$class); } sub print { my $fh=shift; print @_; print $fh "LOG:",@_; } package main; my $p=MyPrint->new('/dev/null'); print $p "TEST";
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#!/usr/bin/perl use strict; use warnings; # Paket zum kopieren aller Ein/Ausgaben {package PerlIO::via::mycopy; my $copyfile=undef; sub copyfile { my $f=shift(); # ignoriere die Übergeben Klasse oder Objekt $f=shift() if(defined($f) && (ref($f) eq __PACKAGE__ || $f eq __PACKAGE__) ); $copyfile = $f if(defined $f); return $copyfile; } sub PUSHED { my ($class,$mode,$fh)=@_; my $cf=$copyfile; return -1 unless(defined($cf)); if(ref($cf) ne 'GLOB') { $cf=undef; open($cf, '>', $copyfile) || return -1; } print $cf "Start Log at ".localtime()."\n"; return bless($cf,$class); } sub FILL { my ($myfh,$fh)=@_; if (defined( my $line=readline($fh) )) { print $myfh "IN: $line"; return $line; } return undef; } sub WRITE { my ($myfh,$buf,$fh)=@_; print $myfh "OUT: $buf"; return -1 unless(print $fh $buf); return length($buf); } 1;} package main; # entweder ein Filehande #open(my $log, '>', 'log.txt') or die "Error open Log ($!)\n"; #PerlIO::via::mycopy->copyfile($log); # oder ein Dateiname #PerlIO::via::mycopy->copyfile('log.txt'); # nach STDERR PerlIO::via::mycopy->copyfile(\*STDERR); { # STDOUT über mycopy umleiten open(my $stdout, ">&STDOUT"); close(STDOUT); open (STDOUT, ">&:via(mycopy)", $stdout) or die "Unable to mycopy standard output: $!\n"; } print "TEST1\n"; print "TEST2\n"; print "TEST3\n";