Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by kennethk (Abbot)
on Dec 29, 2009 at 21:07 UTC ( [id://814822]=note: print w/replies, xml ) Need Help??


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

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.

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

    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?

        Well that's a major exaggeration. The cost of loading IO::Handle would be quite small if it was rewritten in C. (100 bytes?) In fact, there already exists modules whose methods are built right into Perl: utf8, version and UNIVERSAL, for example.

        Not the entire Core, but I'm surprised that the module for an available object wouldn't be loaded to assist handling that object.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-19 18:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found