in reply to Trouble making my own open()-like routines.

You really should be using lexical filehandles instead of bareword filehandles, especially if you're passing things around. That aside, you can solve your problem by either declaring the test sub before the print statement, or by using &test() to disambiguate the sub call.

print's prototype is extremely complicated to begin with. I think there are three ways to interpret a statement like this:

print FOO test();
When the compiler gets to this point, it has no prior hints about FOO or test. It guesses indirect object notation -- not a horrible guess when two consecutive tokens are barewords. Apparently the compiler keeps track of earlier open BAREWORD statements to hint to the compiler that the bareword refers to a filehandle. I believe that's why changing to a normal open works fine. Declaring the sub test before the open statement hints to the compiler that test as a bareword refers to a sub, not a class name. Using &test() is also unambiguously a call to a subroutine. (You can always see which syntax the compiler chose by using Deparse)

I believe this is one of the reasons tye often speaks out strongly against /^[a-z]+$/ function names. You've shown that the compiler doesn't always get it right. Update: This particular problem still happens if you rename test to Te_st...

blokhead

Replies are listed 'Best First'.
Re^2: Trouble making my own open()-like routines.
by William G. Davis (Friar) on Jul 07, 2004 at 23:47 UTC

    You really should be using lexical filehandles instead of bareword filehandles, especially if you're passing things around. That aside, you can solve your problem by either declaring the test sub before the print statement, or by using &test() to disambiguate the sub call.

    Yeah, I suppose I shouldn't be using those bareworded file handles. Predeclaring or just not using subroutines in conjunction with print isn't a huge deal as these two open*() routines belong to a specific Perl application coded by me, not some public Perl module, so a less-flexible interface is acceptable.

    Apparently the compiler keeps track of earlier open BAREWORD statements to hint to the compiler that the bareword refers to a filehandle. I believe that's why changing to a normal open works fine.

    That's not fair! :(