http://www.perlmonks.org?node_id=1089335

gupr1980 has asked for the wisdom of the Perl Monks concerning the following question:

We call perl scripts from within a IBM tool. The way we do this is make a call to JAVA runtime process and run a perl command on the server, the java process returns the output/error of the PERL script. In our testing we found at times the java process just hangs. Looks like the PERL script doesnt throw any output or error and just quits?? code is below
=cut use strict; use warnings; my $infilename = $ARGV[0]; my $outfilename = $ARGV[1]; my $header = $ARGV[2]; my $readoffset = $ARGV[3]; my $readreccnt = $ARGV[4]; my $inreccnt = 0; my $outreccnt = 0; print "StartTime:"; print scalar localtime(time); open my $INFILE, '<', $infilename; seek $INFILE, $readoffset, 0; open my $OUTFILE, '>>', $outfilename; if ($artheader ne 'NOHDR'){ $artheader =~ tr/'^'/' '/; print $OUTFILE "$header\n"; $outreccnt++; } READLOOP: while (<$INFILE>){ $inreccnt++; if ($inreccnt > $readreccnt){ last READLOOP; } s/^HHH\.S.*//s; s/^EEE\.S.*//s; s/^0000\S{4}\s{2}//; print $OUTFILE $_; $outreccnt++; } close $INFILE; close $OUTFILE; print ",EndTime:"; print scalar localtime(time); print ",\n"; print "InFileName:"; print $infilename; print ",ReadOffset:"; print $readoffset; print ",ReadRecordCnt:"; print $readreccnt; print ",OutFileName:"; print $outfilename; print ",OutRecordCnt:"; print $outreccnt; print ",\n"; print "HeaderLen:"; print (length $artheader); print ",HeaderData:"; print $header; print ",\n";
earlier i had use autodie but then took it out. I am new to PERL, is there a case where a PERL script can just quit and not throw any. Should i use "use autodie"?

Replies are listed 'Best First'.
Re: can PERL just quit?
by Eily (Monsignor) on Jun 09, 2014 at 21:39 UTC

    Your code doesn't compile, because $artheader isn't defined anywhere. I saw that by putting your code in a file called test.pl and running perl -c test.pl. This checks the syntax of the program without actually running it so you can try perl -c on your file without side effects.

    Edit: this means you should try and see why your tool doesn't give you any feedback on this error. The compilation error message should be sent to the standard error output, just as any error occuring during runtime.

      Good catch... but a framework running Perl scripts (like eg Tivoli) should be able to report compilation problems...(?)

      Cheers Rolf

      (addicted to the Perl Programming Language)

        I guess it should yes. At least gupr1980 knows the problem is not on perl's side. I'm going to edit my post.

      It's just a typo. I was trying to change artheader to header for this post. No compile errors.
Re: can PERL just quit?
by LanX (Saint) on Jun 09, 2014 at 21:37 UTC
    Isn't this rather an OS question?

    Are you sure the Perl script terminated?

    Maybe try

    • logging a heartbeat
    • overwriting $SIG{__DIE__}
    • putting debug code into END{block}
    (the later 2 might (?) be redundant :)

    I'm always inclined to blame JAVA though...

    Cheers Rolf

    (addicted to the Perl Programming Language)

Re: can PERL just quit?
by LanX (Saint) on Jun 09, 2014 at 22:45 UTC
    Regarding autodie :

    Those frameworks normally use quite antique Perl versions and installing modules shouldn't be easy.

    But the documentation for open already has it all:

    open(my $fh, "<", "input.txt") or die "cannot open < input.txt: $!";

    and so on for print and close

    you said your JAVA-process hangs...

    ...IF the Perl process was really erminated I can't see a sane reason for this, except that the JAVA process is faulty.

    Consider file system problems or race conditions...

    Otherwise call IBM support.

    Cheers Rolf

    (addicted to the Perl Programming Language)

Re: can PERL just quit?
by boftx (Deacon) on Jun 09, 2014 at 21:24 UTC

    Since you are not testing the return values of open or anything else, I would definitely have use autodie; in place.

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
      i get Can't locate autodie.pm on Linux. How can i get this? I have autodie.pm on my windows workstation, is there a way i can copy it over to Linux server?
        Hmm, surprising... The autodie module has been a standard Perl package for quite a while. Which version of Perl are you running?

        You can get it from metaCPAN: autodie

        I would normally say get it from CPAN, but they have been down a lot lately. :(

        You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
        You don't need autodie. You can rewrite your code so that it checks for errors and reports the reason for failure, e.g.:
        open my $INFILE, '<', $infilename or die "Cannot open $infilename: $!" +; seek $INFILE, $readoffset, 0 or die "Cannot seek: $!"; open my $OUTFILE, '>>', $outfilename or die "Cannot open $outfilename: + $!";
Re: can PERL just quit?
by sundialsvc4 (Abbot) on Jun 09, 2014 at 21:40 UTC

    Another way to do it would be to put an eval block around more-or-less “all of it,” then check the result afterward.   If the process throws a runtime error, it will be caught here, and you can output something that will help you to resolve the problem.   In any case, you can force the Perl script to generate some STDERR output in all cases, which is what the Java wrapper seems to be looking for.

    Sometimes I define a variable outside of the eval-block, called $doing_what which I populate with some string (initially "nothing"), changing it along the way, specifically to use it in any caught error-messages.   Whatever the string is found to contain will at least indicate that the failure occurred very-shortly after the statement which set the variable to this particular value.   While it might be a low-tech way to do it, it works well.   Just be very sure that no statement which assigns a value to that string can itself encounter a runtime error, and that the variable is never undef.

      Putting autodie sounds simpler, will it throw an error on all cases?
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: can PERL just quit?
by blue_cowdawg (Monsignor) on Jun 10, 2014 at 18:05 UTC
        is there a case where a PERL script can just quit

    No, slaves can't quit. They have to be sold. :-P


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg