Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Lexical filehandles: unblessed but (sometimes) can

by ikegami (Patriarch)
on Mar 15, 2018 at 19:44 UTC ( [id://1210988]=note: print w/replies, xml ) Need Help??


in reply to Lexical filehandles: unblessed but (sometimes) can

$fh contains a reference to a glob. The glob isn't blessed. However, the IO object referenced by the glob is blessed.

$ perl -MScalar::Util=blessed,reftype -E' open my $fh, ">", \my $buf; say reftype($fh) // "[not a ref]"; say blessed($fh) // "[not blessed]"; say reftype(*$fh{IO}) // "[not a ref]"; say blessed(*$fh{IO}) // "[not blessed]"; ' GLOB [not blessed] IO IO::File

Replies are listed 'Best First'.
Re^2: Lexical filehandles: unblessed but (sometimes) can
by vr (Curate) on Mar 15, 2018 at 21:02 UTC
    use strict; use warnings; use feature qw/ say /; use Scalar::Util qw/ blessed/; open my $fh, ">", undef; say blessed( *$fh{ IO }); # IO::File say *$fh{ IO }-> can( "print" ) ? "yes" : "no"; # no autoflush $fh 1; say *$fh{ IO }-> can( "print" ) ? "yes" : "no"; # yes

    And:

    use strict; use warnings; use Log::Log4perl qw / :easy /; use Log::Dispatch::Handle; open my $fh, ">", undef; #autoflush $fh 1; my $app = Log::Log4perl::Appender-> new( "Log::Dispatch::Handle", handle => *$fh{ IO }); __END__ The IO::File class is missing the 'print' method

    And uncommenting the comment makes the program run OK and error message gone.

      That's because IO::File is loaded on demand. (One used to have to load it explicitly.) You can change the output to yes-yes by explicitly loading it.

        Simple answer to seemingly complex problem. Thank you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-26 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found