Perl threads are a big and rather cludgy hammer, and should only be used when you need to share lots of data between many processes. They're great to have when you truly need them, but in your case, you don't truly need them. :)
Since you know exactly how long you want to wait, you can simply use the alarm function. You've got high precision for your timing, so you should probably use the Time::HiRes module, which is almost certainly a core module for you. To get all of this to work, a skeleton of your code might look like this:
use Time::HiRes qw(alarm); # Pre-declare any variables you'll need in play_func and in # the main code my %all_files; sub play_func { ## Most of your originally posted code goes here, but ## replace the call to sleep with this: alarm ($song_length); } # You'll need to associate the alarm signal handler with # play_func like so: $SIG{ALRM} = \&play_func; # Initialize your data here, such as your playlist # Kick-off the play loop by calling play_func once: play_func(); # Put your own looping/interface code here: while(1) { # This will loop until you call 'last;' or exit. # When the alarm signal goes off, Perl will jump to # your alarm-handling code and execute that, then # return to whatever it was doing in here. # This will lead to a slight interrupt in this loop, # but it probably won't be noticeable. }
Hope that's a useful alternative approach!
In reply to Consider using alarms instead of threads
by pdltiger
in thread Looping in the background of your main program
by Baphi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |