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

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

by gmargo (Hermit)
on Dec 29, 2009 at 20:52 UTC ( #814817=note: print w/replies, xml ) Need Help??


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

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.

  • Comment on Re^3: Regarding STDOUT and indirect object notation for print

Replies are listed 'Best First'.
Re^4: Regarding STDOUT and indirect object notation for print
by kennethk (Abbot) on Dec 29, 2009 at 21:07 UTC
    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.

        Would you want perl to load the entire Core module set before running a one-liner?
Re^4: Regarding STDOUT and indirect object notation for print
by romandas (Pilgrim) on Dec 29, 2009 at 21:00 UTC
    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).

        Heh.. not the first time I've been all wrong; likely not the last.

        So, STDOUT is a glob? Of what? My understanding of globs is it's a bunch of filenames, which you can then iterate over to open, close, test, etc. Is that wrong too?

        I'm not sure I follow your logic on the last statement. If, for a glob, Perl will check the IO::Handle namespace, then loading the IO::Handle module will populate that namespace with what Perl needs to find (in this case, the print method), yes? If not, please clarify your statements; I'd appreciate it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://814817]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2017-12-18 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (495 votes). Check out past polls.

    Notices?