The easiest way (IMHO) is to fork a 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
use strict;
use warnings;
print "Start of script\n";
run_sleep();
print "End of script\n";
sub run_sleep {
my $pid = fork;
return if $pid; # in the parent process
print "Running child process\n";
select undef, undef, undef, 5;
print "Done with child process\n";
exit; # end child process
}
If you run this in your shell, you'll see output that looks something like this:
Start of script
End of script
Running child process
(wait five seconds)
Done with child process
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.
run_sleepto run in a new process.