Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

flock with anonymous filehandle

by Xxaxx (Monk)
on Jun 09, 2001 at 04:56 UTC ( [id://87133]=perlquestion: print w/replies, xml ) Need Help??

Xxaxx has asked for the wisdom of the Perl Monks concerning the following question:

I'm learning (or trying to learn) how to work with filehandles passed along through returns and params.

I believe I have the fileopen and return working. At least is seems to be working.

The part I'm messing up on is the file locking part.

From flock manpage:

sub lock { flock(MBOX,LOCK_EX); # and, in case someone appended # while we were waiting... seek(MBOX, 0, 2); } sub unlock { flock(MBOX,LOCK_UN); } open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}") or die "Can't open mailbox: $!"; lock(); print MBOX $msg,"\n\n"; unlock();
Below was my attempt at making this work. If a kind hearted monk could help unscramble this I'd appreciate it.
sub fileopen { my($filename) = @_; local *MW; my($openokay) = open(MW,"+<$filename"); if (!$openokay) { return 0; } lock(*MW); seek(MW,0,0); return *MW; } sub lock { my($nick) = @_; flock({$nick},LOCK_EX); # and, in case someone appended # while we were waiting... seek({$nick}, 0, 2); } sub unlock { my($nick) = @_; flock({$nick},LOCK_UN); }
Thanks in advance. Claude

Replies are listed 'Best First'.
Re: flock with anonymous filehandle
by wog (Curate) on Jun 09, 2001 at 05:10 UTC
    Your problem is the extra {}s around the filehandle. Although print lets you do that, since it uses a syntax not involving a comma, flock and seek do not. So change like:

    flock({$nick},LOCK_EX);
    to
    flock($nick,LOCK_EX);

    The syntax you had there created a hashref with nothing in it.

    You really should be checking the return value of flock, and open.

    (was updated because I really should've checked what { 'whatever' } produced.)

Re: flock with anonymous filehandle
by HamNRye (Monk) on Jun 09, 2001 at 23:55 UTC

    Just a few more things....

    my($openokay) = open(MW,"+<$filename"); if (!$openokay) { return 0; }

    this could be done much more efficiently:

    open(MW,"+<$filename") || return 0; # Or even... sub fileopen { my ($filename) = shift; if ( open(MW,"+<$filename") ) { lock(*MW); seek(MW,0,0); return *MW; } else { return 0; }

    Also, try using the var = shift syntax for getting arguments in a subroutine. I've run into too many problems trying to use the @_. (Think about it, shift returns the first value of an array.)

    And this is the usual reminder, use Strict, and -w...

    And GOD said unto them, "Dids't thou use strict??"

      Also, try using the var = shift syntax for getting arguments in a subroutine. I've run into too many problems trying to use the @_.

      The syntax used is perfectly fine. The thing that one must remember when using @_ is that:

      my $arg = @_;

      will assign the number of arguments to $arg, but:

      my($arg) = @_;

      will assign the first argument to $arg. (The ()s give list context to the right-hand side of the my assignment.)

      update: Of course, one must also remember to assign all the arguments at once when using @_.

        Thanks for the comments.
        I was wondering though, if the admonition against using
        my($arg) = @_;
        is because some folks forget and leave out the all importand ()?

        Or is there some other reason that the shift is seemingly preferred?

        Thanks
        Claude

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://87133]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2024-04-25 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found