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.


In reply to Trouble making my own open()-like routines. by William G. Davis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.