|Perl: the Markov chain saw|
Filehandles vs. Packages: And the winner is...by converter (Priest)
|on Aug 30, 2001 at 02:37 UTC||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:
The indicated line number was the following attempt to invoke the class method &CGI::new:
I thought perhaps there was some ambiguity about the intent of the method invocation, so I suggested using
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:
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?