William G. Davis has asked for the wisdom of the Perl Monks concerning the following question:

Hi again monks.

I need some help here with two file manipulation routines I'm working on. One is a routine that works just like open() under '>>' mode, only it flock()s the specified handle before returning. The other routine works like open() under '>' mode, only it flock()s the specified handle before clobbering it and then returns. Here are the two routines:

sub openFileForAppending(*$) { my $handle = qualify_to_ref(shift, caller);; my $name = shift; open($handle, '>> ' . $name) or return; attemptFlock($handle, LOCK_EX); return $handle; } ... sub openFileForWriting(*$) { my $handle = qualify_to_ref(shift, caller); my $name = shift; # If a file with that name exists, we'll open it for reading and # writing to prevent clobbering it prematurely before flock()ing i +t. # If we can successfully open it, then we'll try to flock() it, *t +hen* # we can safely clobber it: if (-e $name) { open($handle, '+< ' . $name) or return; attemptFlock($handle, LOCK_EX); seek($handle, 0, 0); truncate($handle, 0); } else { open($handle, '> ' . $name) or return; attemptFlock($handle, LOCK_EX); } return $handle; }

Now, the following works just fine:

openFileForWriting(TEST, 'test.txt'); print TEST "A test.\n"; close TEST;

But the following:

openFileForWriting(TEST, 'test.txt'); print TEST test(); close TEST; sub test { return "Another test.\n" }

Gives me:

Name "main::TEST" used only once: possible typo at listing6.pl line 10.
Can't locate object method "TEST" via package "test" at listing6.pl line 9.

It appears what's happening here is that print TEST test() is being parsed as print test->TEST() (Perl's handy indirect object syntax) for some ungodly reason, yet if I replace the call to my special routine with a call to just plain-old open(), it works just fine. These two routines are defined in a module and exported by default. Is there anyway to get Perl to treat my open*() routines as it would the built-in open() in this respect?

Any help is appreciated.

Replies are listed 'Best First'.
Re: Trouble making my own open()-like routines.
by blokhead (Monsignor) on Jul 07, 2004 at 06:19 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.

    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

      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! :(

Re: Trouble making my own open()-like routines.
by davido (Cardinal) on Jul 07, 2004 at 06:21 UTC

    You probably need, for one thing, to pass the typeglob, not the bareword filehandle. I understand the prototypes, but they need to come before the sub is ever called... and prototypes are an ugly world anyway. Even better, pass a lexical just as you would if you said, "open my $fh, $filename or die $!;".

    And next, you should probably be using the three arg version of open. That's safer.

    And next... you should probably die instead of just returning if you fail to open the file. Currently, when you fail to open the file, you'll never know why. You could die, print $! for details on why you failed.


    Dave

      I think this is a case where the * prototype is justified. The OP is using it (in conjunction with Symbol::qualify_to_ref) correctly.

      With a space after the > or >+, two arg open is safe.

        I'd much prefer to use the three-argument version of open, but sadly, my target platform is Perl 5.005 and the three-argument version of open was introduced with 5.6.1.

        I also don't want to die in the routines themselves because that would limit the amount of information in the error messages. Far better to let the caller handle it, so they can do stuff like this:

        openFileForWriting($fh, 'file.log') or die "Couldn't open log file to store the crash report: $!";