use threads; use threads::shared; my $run : shared = 1; my $thread = threads->new( sub { while ($run) { print "Hi!\n"; sleep 1; } } ); print "Waiting 5 seconds while the thread runs\n"; sleep 5; print "Shutting down\n"; $run = 0; $thread->join;