in reply to Execute a sub in the background

You're doing it the wrong way. Learn about event-driven programming.
use 5.010; use AnyEvent; use Term::ReadKey qw(ReadMode ReadKey); ReadMode('cbreak'); my %events; %events = ( key => AE::io(*STDIN, 0, sub { for (ReadKey) { when ('q') { ReadMode('normal'); $events{quit}->send; + } when ("\n") { say 'ZOMG, ENTER!'; } default { say } } }), quit => AE::cv, ); $events{quit}->recv;
) { ReadMode(

Replies are listed 'Best First'.
Re^2: Execute a sub in the background
by egzoti4en (Initiate) on Feb 01, 2012 at 15:14 UTC

    Well this worked excellent $thr = threads->create(\&status_line)->detach;.

    Thank you very much for this advice.

    Is there a way after the thread is detached to kill the process ?

      You don't have to worry about detached threads when killing the process. They will be taken care of. When a thread started without 'detach'-ing it is still alive when your process is terminated you will get a warning that it exited with unjoined threads. My experience is mainly with Tk: in this case detached threads work fine as far as they never terminate. If they terminate while the MainLoop is still active it will crash miserably. So with Tk I always use infinite while(1){#do processing} style threads.

        I understand that Perl takes care of detached threads, but what if i want to kill at some time in my program before Perl did.