in reply to Re^4: IO::Handle UNIVERSAL::can STDOUT print
in thread IO::Handle UNIVERSAL::can STDOUT print

IO::Handle::can is called with *STDOUT as the first argument just like IO::Handle::print is called with *STDOUT as the first argument. Seeing as *STDOUT isn't an object, can returns false as documented.

It wouldn't hurt for IO::Handle to override can to handle this better, though.

Replies are listed 'Best First'.
Re^6: IO::Handle UNIVERSAL::can STDOUT print (suggested change)
by ikegami (Patriarch) on Sep 04, 2009 at 07:03 UTC
    Suggested change:
    { package IO::Handle; use strict; use warnings; use Scalar::Util qw( ); sub can { my $self = shift; $self = 'IO::Handle' if !Scalar::Util::blessed($self); return $self->SUPER::can(@_); } } use strict; use warnings; $\="\n"; print "\\*STDOUT: ", \*STDOUT; print "*STDOUT: ", *STDOUT; print "*STDOUT{IO}: ", *STDOUT{IO}; print ''; for (1..2) { print "\\*STDOUT: ", (\*STDOUT) ->can('print') ||0; print "*STDOUT: ", *STDOUT ->can('print') ||0; print "*STDOUT{IO}: ", *STDOUT{IO} ->can('print') ||0; print ''; require IO::Handle; }
    \*STDOUT: GLOB(0x182a044) *STDOUT: *main::STDOUT *STDOUT{IO}: IO::Handle=IO(0x182a054) \*STDOUT: 0 *STDOUT: 0 *STDOUT{IO}: 0 \*STDOUT: CODE(0x189cb0c) *STDOUT: CODE(0x189cb0c) *STDOUT{IO}: CODE(0x189cb0c)