I have been coding in perl off/on for two years now and am fairly comfortable with it on Win32 systems. I recently aqcuired responsibility for some *NIX systems and while creating some sample code to teach file locking, i found that it did not work on any (AIX,Solaris) of them. Symptoms are exactly the same:
I cannot get
sleep or
select(undef,undef,undef,250) to work in the while loop; my cursor just sits idle and i do not see the prints to STDOUT that are coded in the loop .... as soon as I comment out sleep/select it works fine.
BTW - this same script works on Win32.
#!/usr/bin/perl -w
use strict;
use Fcntl ':flock';
my $file = '/export/home/smchugh/lock_file.txt';
print FILE "\nI HAVE LOCKED YOU @ @{[scalar localtime]}\n";
open (FILE, ">>$file") || die "Couldn't open $file.\n";
flock(FILE, LOCK_EX | LOCK_NB) || die "Couldn't lock $file.\n";
seek(FILE,0,2);
my $loop_var = 0;
while (1) {
print "File $file is locked." unless $loop_var;
select(undef,undef,undef, 250);
print "." if $loop_var;
$loop_var = 1;
}
Edit Masem 2002-02-19 - Changed title from "losing confidence..."
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.