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

Your code as posted doesn't even compile. When I fix the code as follows, it works for me. So it must be your old version of threads, or your version of Perl, or maybe you are not showing us the real code you use.

use threads; use strict; my $thr = threads->create ( sub { while (1) { eval { while (1) { worker(); } }; if ($@) { print "Error: <$@>.\n"; print "Error Occured: <$@>.\n"; $@=(); sleep (1); } } } ); sub worker { open FILE_OUT, "> /root/xyz" or die "Can't open file: $!"; } $thr->join; __END__ Name "main::FILE_OUT" used only once: possible typo at tmp.pl line 27. Error: <Can't open file: No such file or directory at tmp.pl line 27. >. Error Occured: <Can't open file: No such file or directory at tmp.pl l +ine 27. >. Error: <Can't open file: No such file or directory at tmp.pl line 27. >. Error Occured: <Can't open file: No such file or directory at tmp.pl l +ine 27. >. Error: <Can't open file: No such file or directory at tmp.pl line 27. >. Error Occured: <Can't open file: No such file or directory at tmp.pl l +ine 27. >. Terminating on signal SIGINT(2)
C:\Projekte>perl -V Summary of my perl5 (revision 5 version 10 subversion 0) configuration +: Platform: osname=MSWin32, osvers=5.1, archname=MSWin32-x86-multi-thread uname='' config_args='undef' hint=recommended, useposix=true, d_sigaction=undef useithreads=define, usemultiplicity=define useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=und +ef use64bitint=undef, use64bitall=undef, uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='gcc', ccflags =' -s -O2 -DWIN32 -DHAVE_DES_FCRYPT -DPERL_IMPL +ICIT_CONTEXT -DPERL_IMPLICIT_SYS -fno-strict-aliasing -DPERL_M SVCRT_READFIX', optimize='-s -O2', cppflags='-DWIN32' ccversion='', gccversion='3.4.5', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234 d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=12 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='long lo +ng', lseeksize=8 alignbytes=8, prototype=define Linker and Libraries: ld='g++', ldflags ='-s -L"C:\strawberry\perl\lib\CORE" -L"C:\straw +berry\c\lib"' libpth=C:\strawberry\c\lib libs= -lmsvcrt -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool - +lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 - luuid -lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 perllibs= -lmsvcrt -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspo +ol -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi 32 -luuid -lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 libc=-lmsvcrt, so=dll, useshrplib=true, libperl=libperl510.a gnulibc_version='' Dynamic Linking: dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' ' cccdlflags=' ', lddlflags='-mdll -s -L"C:\strawberry\perl\lib\CORE +" -L"C:\strawberry\c\lib"' Characteristics of this binary (from libperl): Compile-time options: MULTIPLICITY PERL_DONT_CREATE_GVSV PERL_IMPLICIT_CONTEXT PERL_IMPLICIT_SYS PERL_MALLOC_WRAP PL_OP_SLAB_ALLOC USE_ITHREADS USE_LARGE_FILES USE_PERLIO Built under MSWin32 Compiled at Aug 11 2008 04:41:33 @INC: C:/strawberry/perl/lib C:/strawberry/perl/site/lib . C:\Projekte>perl -Mthreads -e "die $threads::VERSION" 1.73 at -e line 1.

Replies are listed 'Best First'.
Re^4: Eval thread and restart it.
by mr_p (Scribe) on Apr 19, 2010 at 20:24 UTC
    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; }

      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

      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.