in reply to Re: Perl script crashing at lockfile ?
in thread Perl script crashing at lockfile ?

Hi tachyon,

Thanks for sorting out the lockfile problem. I will either use this one you supplied

my $got_lock; use Fcntl; # to get constants for O_CREAT | O_EXCL | O_RDWR for ( 0 .. 5 ) { if ( sysopen(my $fh, "$base_dir/.lock", O_CREAT | O_EXCL | O_RDWR, + 0600) ) { $got_lock = 1; close $fh; last; } sleep 2; } diehtml("Lock error $!\n") unless $got_lock;

.. or the "fully blown" lockfile code you supplied in your other post ..You either have to delete it by hand or do something like this:

I consider the lockfile problem sorted out now, thanks. Because of the problems now with "opens", I would to replace code like this:

open(ORDERFILE, ">$outfile") or diehtml("Can't open order records: $!\n");

with (native) code of the format:

sysopen($fh, "$dir/$name", O_CREAT | O_EXCL | O_RDWR, 0600);

... would it be something like:

$fh = "ORDERFILE"; sysopen($fh, "$outfile", O_CREAT | O_EXCL | O_RDWR, 0600) or diehtml("Can't open order records: $!\n");

Just a wild guess. :)

Peter

Replies are listed 'Best First'.
Re: Re: Re: Perl script crashing at lockfile ?
by tachyon (Chancellor) on Oct 02, 2003 at 14:40 UTC

    No :-)

    This code is fine for what it does:

    open # open a file for read or write ( ORDERFILE # onto a FILEHANDLE called ORDERFILE , " > # > means write to the file, create if does not exist # Note all previous content will be deleted # if this file exists >> means append to the end $outfile # full or relative path to file " ) or # if the open works it returns true so we never do the next + bit diehtml() # print an error message

    You don't wanna touch anything in the demon code. It was for illustrative purposes and is not relevant to your problem provided you are happy that the code you have worked just replace the lockfile code.

    In this code:

    $fh = "ORDERFILE"; sysopen($fh, "$outfile", O_CREAT | O_EXCL | O_RDWR, 0600) or diehtml("Can't open order records: $!\n");

    You don't need to set $fh to a string. It will just get overwritten in the open. A FILEHANDLE is a type of perl internal object (like a scalar) and the reference to it is held in the $fh var. To illustrate the syntax:

    open $fh, ">$file" or die "Can't write $file $!\n"; print $fh "here is some data\n"; close $fh;

    Same with sysopen but I can't be bothered to type that much.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print