Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

can PERL just quit?

by gupr1980 (Novice)
on Jun 09, 2014 at 21:20 UTC ( #1089335=perlquestion: print w/ replies, xml ) Need Help??
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"?

Comment on can PERL just quit?
Download Code
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?

        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.
        Hmm, surprising... The autodie module has been a standard Perl package for quite a while. Which version of Perl are you running?
        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 LanX (Canon) 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 Eily (Deacon) 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 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?

        My blunt “take” on the situation :-/ is that what you really want to do here is to “get ’er done” and move on.   The bottom-line is that you want to “handle any eventuality” in a way that you know that your Java-wrapper code will understand.   And, you’d really like to get it done today.   Thus, I suggest that you take matters into your own hands:   wrap-up the whole ball of wax in an envelope that will catch anything that happens, and follow it with code that is assured to satisfy the Java wrapper’s requirements no matter what happens.   This is somewhat of a specialized situation.   Get ’er done.   “Shrink-wrap it and ship it.”   Leave generalities for another day.

        And, worst case ... I mean ... look at the actual source-code to autodie.   It’s frippin’ tiny ...   As they said in the Broadway show, “I can do that!”

Re: can PERL just quit?
by LanX (Canon) 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 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1089335]
Approved by boftx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-12-19 13:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (83 votes), past polls