Is there a way to run a perl subroutine in the background? I’ve looked around and seen some mentions in regards to threads but it would help to see an example, or point me in the right direction. Thanks.
Would like to run run_sleep in the background.
#!/usr/bin/perl
print "Start of script";
run_sleep();
print "End of script";
sub run_sleep {
select(undef, undef, undef, 5); #Sleep for 5 seconds then do whatever
}
The easiest way (IMHO) is to
forka subprocess and let that do the work. Perl threads can be painful so I try to avoid them whenever possible.Here’s a simple example
If you run this in your shell, you’ll see output that looks something like this:
(wait five seconds)
The parent process will exit immediately and return you to your shell; the child process will send its output to your shell five seconds later.
If you want the parent process to stay around until the child is done, then you can use
waitpid.