in reply to Re^3: Eval thread and restart it.
in thread Eval thread and restart it.

I was able to re-write and get eval to work in a sample program. Thanks.

Do you have any other suggestion on what I'm am trying to do. Is there another way but eval?

Here is the code I have:
#!/usr/bin/perl use lib "/nss/nfx/thread_libs/lib/"; use threads; my $thr = threads->create( sub { while (1) { eval { while (1) { print "Starting Main function again.\n"; worker(); } }; if ($@) { print "Error Occured: <$@>.\n"; $@=(); sleep (5); } } } ); sub worker { open FILE_OUT, "> /root/xyz" or die "Can't open file: $!"; } while (1) { sleep 500; }

Replies are listed 'Best First'.
Re^5: Eval thread and restart it.
by ikegami (Patriarch) on Apr 19, 2010 at 20:40 UTC

    Is there another way but eval?

    To do what? Catch exceptions? No. To make exceptions non-fatal? You could execute the code in a child process

Re^5: Eval thread and restart it.
by BrowserUk (Patriarch) on Apr 19, 2010 at 21:37 UTC
    Do you have any other suggestion on what I'm am trying to do.

    Without you showing us what you are doing inside your worker() sub, it's pretty hard to suggest a better way of doing it.

    In general, it would be better to catch exceptions inside the sub rather than just blindly restarting it from scratch. The likelyhood is that you'll just keep retrying the same thing over and over, and never get anything done.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.