Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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?


Replies are listed 'Best First'.
(tye)Re2: Filehandles vs. Packages: And the winner is...
by tye (Sage) on Aug 30, 2001 at 22:40 UTC
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...

    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";'


        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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://108936]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2017-03-26 20:49 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (315 votes). Check out past polls.