FAQ 5.21 I still don't get locking. I just want to increment the number in the file. How can I do this?


This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest ...
Posted On: Tuesday 27th of November 2012 11:51:01 PM Total Views:  533
View Complete with Replies

RELATED TOPICS OF Perl PROGRAMMING LANGUAGE




Application crashing with error 'Helvetica -12 bold' still in cashe

Hi , Please note that while closing my application, some times it crashes. It prints following error while crashing. -------------------------------------------------------------- Font Helvetica -12 bold still in cache. -------------------------------------------------------------- It seems that it is crashing due to memory leak in Tk. Please have a look at the stack trace. Any help in this regard is highly appreciated.
VIEWS ON THIS POST

164

Posted on:

Tuesday 16th October 2012
View Replies!

FAQ 5.22 All I want to do is append a small amount of text to the end of a file. Do I still have to use locking?

This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.22: I want to do is append a small amount of text to the end of a file. Do I still have to use locking If you are on a system that correctly implements flock() and you use the example appending code from "perldoc -f flock" everything will be OK even if the OS you are on doesn't implement append mode correctly (if such a system exists.) So if you are happy to restrict yourself to OSs that implement flock() (and that's not really much of a restriction) then that is what you should do. If you know you are only going to use a system that does correctly implement appending (i.e. not Win32) then you can omit the seek() from the code in the previous answer. If you know you are only writing code to run on an OS and filesystem that does implement append mode correctly (a local filesystem on a modern Unix for example), and you keep the file in block-buffered mode and you write less than one buffer-full of output between each manual flushing of the buffer then each bufferload is almost guaranteed to be written to the end of the file in one chunk without getting intermingled with anyone else's output. You can also use the syswrite() function which is simply a wrapper around your systems write(2) system call. There is still a small theoretical chance that a signal will interrupt the system level write() operation before completion. There is also a possibility that some STDIO implementations may call multiple system level write()s even if the buffer was empty to start. There may be some systems where this probability is reduced to zero. -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
VIEWS ON THIS POST

144

Posted on:

Wednesday 17th October 2012
View Replies!

Determine whether a thread is still running in Perl 5.8.8 with "interpreter threads"

Given the following snippet: ----------------------- #!/usr/bin/env perl use threads; sub threadfunc { my $i = 0; while ($i < 3) { shift; print; $| = 1; $i+ +; sleep(1); } } my $thread1 = threads->create(\&threadfunc, '.'); my $thread2 = threads->create(\&threadfunc, '#'); print "Threads created.\n"; while (threads->list() > 0) { for my $thread (threads->list()) { next unless $thread->running(); # how can this be done print "Joining thread: $thread (" . $thread->tid() . ")\n"; $thread->join(); } print "Waiting...\n"; sleep 1; } print "Done.\n"; ----------------------- How can I check whether a thread is still running Note the comment in the code at "next unless $thread->running();". I realize that this is not necessary in this situation, but this is only a test case. Any hints -Samuel
VIEWS ON THIS POST

112

Posted on:

Friday 26th October 2012
View Replies!

How to time out a forked command but still see output?

The goal... perl script launches the external program, shows all its ouput in realtime. If too much time expires, perl script exits, redirects all output of external program to some file. Can someone give me a nudge in the right direction I tried a few things involving alarm() and eval but couldn't get them to work. I won't waste your time with everything that failed, but the following code is as close as I got. It is not acceptable because it doesn't show the output of the program. I also tried something like redirecting CMD to STDOUT but when I do that, the output of the program keeps scrolling to the term even after the timeout. I tried to solve this by closing these filehandles and redirecting them to /dev/null outside of the eval, but those didn't work either. Help is appreciated... #!/usr/bin/perl $command = shift @ARGV; $startupWait = shift @ARGV || 60; eval { local $SIG{'ALRM'} = sub { die "\nTimed out command $command after waiting $startupWait seconds\n"; }; alarm($startupWait); print "Running command: $command\n"; print "with timeout of $startupWait\n"; open(CMD, "$command|"); (@output) = (); close CMD; alarm 0; print "Command completed, output is:\n"; print map { "$_\n" } @output; }; die "$@" if ($@);
VIEWS ON THIS POST

141

Posted on:

Friday 26th October 2012
View Replies!

FAQ 5.23 All I want to do is append a small amount of text to the end of a file. Do I still have to use locking?

This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.23: I want to do is append a small amount of text to the end of a file. Do I still have to use locking If you are on a system that correctly implements flock() and you use the example appending code from "perldoc -f flock" everything will be OK even if the OS you are on doesn't implement append mode correctly (if such a system exists.) So if you are happy to restrict yourself to OSs that implement flock() (and that's not really much of a restriction) then that is what you should do. If you know you are only going to use a system that does correctly implement appending (i.e. not Win32) then you can omit the seek() from the code in the previous answer. If you know you are only writing code to run on an OS and filesystem that does implement append mode correctly (a local filesystem on a modern Unix for example), and you keep the file in block-buffered mode and you write less than one buffer-full of output between each manual flushing of the buffer then each bufferload is almost guaranteed to be written to the end of the file in one chunk without getting intermingled with anyone else's output. You can also use the syswrite() function which is simply a wrapper around your systems write(2) system call. There is still a small theoretical chance that a signal will interrupt the system level write() operation before completion. There is also a possibility that some STDIO implementations may call multiple system level write()s even if the buffer was empty to start. There may be some systems where this probability is reduced to zero. -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
VIEWS ON THIS POST

124

Posted on:

Sunday 4th November 2012
View Replies!

File still in use after "close FILE1"

HI,my program writes to handle FILE1 and closes is properly once I'm done.However the created file remains read only till I exit the whole application...Why would it happen Long live king Moshiach ! http://www.noahide.org/
VIEWS ON THIS POST

105

Posted on:

Sunday 4th November 2012
View Replies!

FAQ 5.22 I still don't get locking. I just want to increment the number in the file. How can I do this?

This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.22: I still don't get locking. I just want to increment the number in the file. How can I do this Didn't anyone ever tell you web-page hit counters were useless They don't count number of hits, they're a waste of time, and they serve only to stroke the writer's vanity. It's better to pick a random number; they're more realistic. Anyway, this is what you can do if you can't help yourself. use Fcntl qw(EFAULT :flock); sysopen(FH, "numfile", O_RDWR|O_CREAT) or die "can't open numfile: $!"; flock(FH, LOCK_EX) or die "can't flock numfile: $!"; $num = || 0; seek(FH, 0, 0) or die "can't rewind numfile: $!"; truncate(FH, 0) or die "can't truncate numfile: $!"; (print FH $num+1, "\n") or die "can't write numfile: $!"; close FH or die "can't close numfile: $!"; Here's a much better web-page hit counter: $hits = int( (time() - 850_000_000) / rand(1_000) ); If the count doesn't impress your then the code might. :-) -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
VIEWS ON THIS POST

127

Posted on:

Sunday 4th November 2012
View Replies!

Font -12 Helvetica still in cache

Hi all , Recently I am coming across an issue where, when I exit from from my application , it shows - Font -12 Helvetica still in cache. The perl version that I am using is 5.8.3 and the Tk version is 804.027. Pls let me know how to overcome this issue.
VIEWS ON THIS POST

116

Posted on:

Sunday 4th November 2012
View Replies!

FAQ 5.23 All I want to do is append a small amount of text to the end of a file. Do I still have to use locking?

This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.23: I want to do is append a small amount of text to the end of a file. Do I still have to use locking If you are on a system that correctly implements flock() and you use the example appending code from "perldoc -f flock" everything will be OK even if the OS you are on doesn't implement append mode correctly (if such a system exists.) So if you are happy to restrict yourself to OSs that implement flock() (and that's not really much of a restriction) then that is what you should do. If you know you are only going to use a system that does correctly implement appending (i.e. not Win32) then you can omit the seek() from the code in the previous answer. If you know you are only writing code to run on an OS and filesystem that does implement append mode correctly (a local filesystem on a modern Unix for example), and you keep the file in block-buffered mode and you write less than one buffer-full of output between each manual flushing of the buffer then each bufferload is almost guaranteed to be written to the end of the file in one chunk without getting intermingled with anyone else's output. You can also use the syswrite() function which is simply a wrapper around your systems write(2) system call. There is still a small theoretical chance that a signal will interrupt the system level write() operation before completion. There is also a possibility that some STDIO implementations may call multiple system level write()s even if the buffer was empty to start. There may be some systems where this probability is reduced to zero. -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
VIEWS ON THIS POST

133

Posted on:

Monday 5th November 2012
View Replies!

file.lock")?" style="color:#1385b4;text-transform:uppercase;" name="Post_237377"> FAQ 5.21 Why can't I just open(FH, ">file.lock")?

This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.21: Why can't I just open(FH, ">file.lock") A common bit of code NOT TO USE is this: sleep(3) while -e "file.lock"; # PLEASE DO NOT USE open(LCK, "> file.lock"); # THIS BROKEN CODE This is a classic race condition: you take two steps to do something which must be done in one. That's why computer hardware provides an atomic test-and-set instruction. In theory, this "ought" to work: sysopen(FH, "file.lock", O_WRONLY|O_EXCL|O_CREAT) or die "can't open file.lock: $!"; except that lamentably, file creation (and deletion) is not atomic over NFS, so this won't work (at least, not every time) over the net. Various schemes involving link() have been suggested, but these tend to involve busy-wait, which is also less than deable. -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
VIEWS ON THIS POST

41

Posted on:

Wednesday 7th November 2012
View Replies!

file.lock")?" style="color:#1385b4;text-transform:uppercase;" name="Post_237552"> FAQ 5.21 Why can't I just open(FH, ">file.lock")?

This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest ...
VIEWS ON THIS POST

35

Posted on:

Saturday 10th November 2012
View Replies!

Error.pm still maintained?

Hi: I'm finding some bugs in Error.pm, and searches for Error on the perl groups return only older (+1Y) postings. Is Error.pm still being maintained If not, is there a better try/catch/finally for perl5 If so, what Thank you. -- ...
VIEWS ON THIS POST

107

Posted on:

Saturday 10th November 2012
View Replies!

tk804 Tk::Listbox insert() still leaking?

Hi all, I recently noticed a perl/tk application of mine is leaking memory. I assumed user error until I came across a post mentioning previous leaks in Tk-804.025_beta9's listbox code. The following test case reproduces the leak on ActivePerl: ==================================== ...
VIEWS ON THIS POST

126

Posted on:

Thursday 15th November 2012
View Replies!

FAQ 5.22 All I want to do is append a small amount of text to the end of a file. Do I still have to use locking?

This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest ...
VIEWS ON THIS POST

156

Posted on:

Monday 19th November 2012
View Replies!

Lexical arrays still 2.5x slower in 5.8.1

I already lifted this question several times. So the only purpose of this posting is to show that this bug is not yet fixed. If you access lexical arrays with a constant key (which is not pathologically large), the access ...
VIEWS ON THIS POST

161

Posted on:

Monday 19th November 2012
View Replies!

FAQ 5.22 I still don't get locking. I just want to increment the number in the file. How can I do this?

This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest ...
VIEWS ON THIS POST

123

Posted on:

Sunday 25th November 2012
View Replies!

lwp::simple get (why it would stop working along with wget when fetch still works)

Hi Perl People, Something recently changed on a site that I was fetching and parsing from with lwp::simple. Here is the thing: For the longest time I was using get() to grab a http: site and store it in a ...
VIEWS ON THIS POST

150

Posted on:

Monday 26th November 2012
View Replies!

dialogBox without widget reference

Is it possible to call a dialog box without a widget reference I want to have a standard input box function that I can call from any function without that function having to have a widget passed in and without ...
VIEWS ON THIS POST

248

Posted on:

Monday 26th November 2012
View Replies!

Socket, cannot get clean results.

, I am working on an interactive server/client setup, not using INET::Sockets. Everything is gravy except for a little leftover stuff being shown. I type a command w/ args, CLIENT join()s the command with args and sends the newstring,the SERVER ...
VIEWS ON THIS POST

151

Posted on:

Monday 26th November 2012
View Replies!

get the return status

hi in unix, i have a program called program_a and i use the system call to invoke it system("program_a"); How do i get the return status of program_a if program_a returns a few status codes...
VIEWS ON THIS POST

159

Posted on:

Monday 26th November 2012
View Replies!