[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Cross-platform solution?



At 13.58 10/14/97, Virtuoso International wrote:
>Folks,
>
>Has anyone found the golden pe(a)rl in terms of file locking and smpt
>mailing that will *work* for all major platforms, i.e. Unix, NT(IIS),
>Windows/DOS and MacOS? I'm programming for Unix on a local Mac which
>generously ignores any Unix sendmail routines but understandably blocks
>on the flock command.  
>
>So far I've written a simple construct to handle Unix and, admittedly
>more as a guess, Windows Mail:
>
>if ($OS eq "UNIX") {
>        $mail_program = "/usr/lib/sendmail -t -n";
>        &PROCESS_MAIL($sender,$to, $subject, $body);
>} elsif ($OS eq "WINDOWS") {
>        $mail_program = "c:/path/dir/blat/blat.exe";
>        ???...
>        &SEND_MAIL($mailprog, $subject, $to, $sender, $body);
>} elsif ($OS eq "MAC") {
>        ???...
>}

You can just use the Net::SMTP module for ALL platforms.  It comes in the libnet distribution.  The MacPerl port of it is on Paul Schinder's page.


>And how about a simple yet secure way to lock files on Mac/Windows
>besides the Unix flock?

Another decent cross-platform solution is to do something like this:

$dbfile = 'mydbm';
$dblock = "$db.lock";
sleep while (-e $dblock);
&lock;
&do_database_stuff;
&unlock;

sub lock {
   open(LOCKFILE,">$dblock") || die $!;
}
sub unlock {
   unlink $dblock;
}

This will basically wait until the lockfile does not exist to continue.  It is an imperfect solution, but good for most situations.  One problem is that if someone else creates a lockfile in the nanosecond between lines 3 and 4, the lock will be defeated.  Another problem is that if the program exits it leaves a lockfile around and it would have to be manually deleted.  One way around it is this:

$locked = 0;

sub lock {
   $locked = 1;
   open(LOCKFILE,">$dblock") || die $!;
}
sub unlock {
   $locked = 0;
   unlink $dblock;
}

END {
   unlink $dblock if ($locked && -e $dblock);
}

That way, your program remembers if it is or is not locked, and most program terminations (including die() and exit()) will call the END block, unlocking if necessary.  Of course, killing the program externally, or reaching a segmentation fault or something, might not call END.

Still, imperfect.  But it should work well for you.

--
Chris Nandor             pudge@pobox.com             http://pudge.net/
%PGPKey=('B76E72AD',[1024,'0824 090B CE73 CA10  1FF7 7F13 8180 B6B6'])
#==            MacPerl:  Programming for the Rest of Us            ==#
#==    Publishing Date: Early 1998. http://www.ptf.com/macperl/    ==#



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch