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

Filehandles vs. Packages: And the winner is...

by converter (Priest)
on Aug 30, 2001 at 02:37 UTC ( #108936=perlquestion: print w/ replies, xml ) Need Help??
converter has asked for the wisdom of the Perl Monks concerning the following question:

A user on the DALnet #perl channel presented an interesting problem today. His CGI program was die'ing with the following exception:

Can't locate object method "new" via package "IO::Handle" (perhaps you + forgot to load "IO::Handle"?) at <line number>;

The indicated line number was the following attempt to invoke the class method &CGI::new:

my $q = new CGI;

I thought perhaps there was some ambiguity about the intent of the method invocation, so I suggested using

my $q = new CGI::;
to force perl to parse 'CGI' as a package name, but to no avail. After looking over the code for twenty minutes or so, I finally noticed that there were several open() statements that opened a filehandle named, you guessed it, 'CGI'. I guessed that CGI was being made an IO::Handle object at compile time, and that the subsquent attempt to invoke the &CGI::new class method was actually being interpreted as an IO::Handle instance method invocation against the non-existent method &IO::Handle::new.

As a test, I moved the invocation of the CGI:: constructor to a BEGIN block, and everything seemed to work as expected.

This demonstrates the problem:

$ perl -MCGI -e 'my $q = new CGI::; open CGI, "~/foo.txt";' Can't locate object method "new" via package "IO::Handle" (perhaps you + forgot to load "IO::Handle"?) at -e line 1.

The obvious workaround is to refrain from using filehandles with the same names as packages, but this is the first time I've run into this problem. I tested this in perl versions 5.005_03, 5.6.0 and 5.6.1, by the way.

Is this breakage in perl, or have I missed a big fat "don't do this!" somewhere in the POD?

conv

Comment on Filehandles vs. Packages: And the winner is...
Select or Download Code
Re (tilly) 1: Filehandles vs. Packages: And the winner is...
by tilly (Archbishop) on Aug 30, 2001 at 03:40 UTC
    It doesn't give as specific of guidance as you might expect, but search for the word WARNING in perlobj. It makes it very clear that the indirect object notation is ambiguous.

    This is why Damian Conway says in his book that you should avoid the indirect object notation...

    UPDATE
    As came out several levels below, I had not understood the error at all. It is far more subtle...

      In other words, the ambiguity is removed if written like so:

      perl -MCGI -e 'my $q = CGI::new(); open CGI, "~/foo.txt";'

      -Blake

        Um, you would want to write:
        my $q = CGI->new();
        Otherwise you have coded a function call, not a method lookup, which means that it is missing its first argument and won't know to search the inheritance hierarchy (if any) looking for the method.
(tye)Re2: Filehandles vs. Packages: And the winner is...
by tye (Cardinal) on Aug 30, 2001 at 22:40 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2014-10-22 21:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (122 votes), past polls