SEARCH YOUR SOLUTION HERE  

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:  651
View Complete with Replies




Related Messages:

Application crashing with error 'Helvetica -12 bold' still in cashe   (234 Views)
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.
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?   (241 Views)
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.
Determine whether a thread is still running in Perl 5.8.8 with "interpreter threads"   (160 Views)
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
How to time out a forked command but still see output?   (185 Views)
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 ($@);
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?   (177 Views)
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.
File still in use after "close FILE1"   (151 Views)
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/
FAQ 5.22 I still don't get locking. I just want to increment the number in the file. How can I do this?   (184 Views)
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.
Font -12 Helvetica still in cache   (161 Views)
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.
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?   (187 Views)
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.
file.lock")?" class="" href="http://www.solveerrors.com/forums/17/perl/faq-5-21-why-cant-i-just-openfh-file-lock-237377" style="color:#333333; font-weight:bold;"> FAQ 5.21 Why can't I just open(FH, ">file.lock")?   (59 Views)
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.
file.lock")?" class="" href="http://www.solveerrors.com/forums/17/perl/faq-5-21-why-cant-i-just-openfh-file-lock-237552" style="color:#333333; font-weight:bold;"> FAQ 5.21 Why can't I just open(FH, ">file.lock")?   (51 Views)
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 ...
Error.pm still maintained?   (151 Views)
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. -- ...
tk804 Tk::Listbox insert() still leaking?   (166 Views)
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: ==================================== ...
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?   (209 Views)
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 ...
Lexical arrays still 2.5x slower in 5.8.1   (252 Views)
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 ...
FAQ 5.22 I still don't get locking. I just want to increment the number in the file. How can I do this?   (201 Views)
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 ...
lwp::simple get (why it would stop working along with wget when fetch still works)   (194 Views)
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 ...
dialogBox without widget reference   (317 Views)
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 ...
Socket, cannot get clean results.   (190 Views)
, 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 ...
get the return status   (226 Views)
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...