Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Regarding STDOUT and indirect object notation for print

by romandas (Pilgrim)
on Dec 29, 2009 at 20:30 UTC ( #814814=note: print w/ replies, xml ) Need Help??


in reply to Re: Regarding STDOUT and indirect object notation for print
in thread Regarding STDOUT and indirect object notation for print

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"? :)


Comment on Re^2: Regarding STDOUT and indirect object notation for print
Re^3: Regarding STDOUT and indirect object notation for print
by gmargo (Hermit) on Dec 29, 2009 at 20:52 UTC

    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.

      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?

        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?

        Unfortunately, it's all wrong.

        • STDOUT isn't blessed. It's not a reference, so it can't be blessed. Globs only act as if they are blessed references.

        • Given a blessed reference or a glob, Perl knows where to look for the methods. For globs, it looks in the IO::Handle namespace.

        • Loading a module doesn't help Perl find anything. It typically loads subroutines. IO::Handle is no exception.

        Perl knows where to look for the method (as indicated by the error message), but there's nothing to find (as indicated in the error message) because IO::Handle wasn't loaded (which isn't indicated in the error message).

      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.

        Thanks.. though, I understood that it (the use statement) would be required for something I explicitly created in code.

        I was just thrown off by the built-in nature of STDOUT and its ilk; I thought perhaps IO::Handle would have already been 'used', thus the methods would've been available, but that somehow I wasn't doing it "right". Thanks for the example.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2014-10-23 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls