in reply to flock with anonymous filehandle

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??"

Replies are listed 'Best First'.
Re: Re: flock with anonymous filehandle
by wog (Curate) on Jun 10, 2001 at 00:06 UTC
    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

        my$arg=shift; gives you the next Argument whether in main or in sub thanks to the magic shift!