Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Regarding STDOUT and indirect object notation for print

by gmargo (Hermit)
on Dec 29, 2009 at 20:24 UTC ( [id://814813]=note: print w/replies, xml ) Need Help??


in reply to Regarding STDOUT and indirect object notation for print

You probably left out "use IO::Handle;".

I get the expected output from the following:

use IO::Handle; STDOUT->print("Hello, World\n"); my $fh = \*STDOUT; $fh->print("Hello, World\n");

Replies are listed 'Best First'.
Re^2: Regarding STDOUT and indirect object notation for print
by romandas (Pilgrim) on Dec 29, 2009 at 20:30 UTC

    Interesting. It didn't seem necessary. Without it, the error still refers to the IO::Handle package. Any reason why it seems to "know" to refer to IO::Handle, yet requires the explicit 'use IO::Handle;' statement?

    According to Intermediate Perl, IO::Handle is being used behind the scenes anyway.. so why the explicit statement? "Just cause"? :)

      A blessed object contains the name of the class, in this case IO::Handle. So the program knows the object's class from the object itself, not by reading the IO::Handle file. To find the object's methods, it must be told where to look.

        A simple implementation to explain gmargo's concept:

        Foo.pm:

        package Foo; use strict; use warnings; sub new { return bless {}; } sub method { return 1; } 1;

        script.pl:

        #!/usr/bin/perl use strict; use warnings; require Foo; my $class = 'Foo'; my $obj = bless {}, $class; print $obj->method;

        Works fine, but if you remove the require Foo; line, you get the error Can't locate object method "method" via package "Foo" at script.pl line 9. perl needs the require statement to find and compile the source files.

        Okay, so even though STDOUT is, by default, a blessed IO::Handle object, and thus possesses the methods described, perl needs to be directed to the IO::Handle module via the 'use' statement so it can find the definitions for those methods. Does that sum it up correctly?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2024-04-18 17:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found