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

Within a perl script I have a C binary which gets executed.
The binary basically does a database connection and fetches data from a database table.
There could be a bug in this binary where sometimes it gives a segmentation fault and does a core dump. In this kind of situation the wrapper perl script aborts execution
and gives segmentation fault message on the command line

What would be a good way to capture this error in a log file through error handling?

Replies are listed 'Best First'.
Re: capture error executing a binary
by graff (Chancellor) on Jun 09, 2005 at 18:10 UTC
    If the C binary is giving you trouble, and all it's doing is pulling stuff out of a database, then you might consider factoring the binary out, and replacing it with DBI (unless there are problems with getting DBI and the appropriate DBD modules installed, in which case, you might want to see what's needed to solve those problems...)

    Apart from that, you don't show any code, so we don't know why the perl script aborts. Maybe it's because the script is written like this:

    my $failure = system( "c_binary ..." ); # system returns exit status die $! if ( $failure ); # which is normally non-zero on failure
    If that's the case, all you need to do is replace "die" with "warn", and redirect the STDERR from your script into a log file -- e.g. if you are using a bash or similar type of shell:
    your_perl_script ... 2>> log.file
      It's an API that comes with the application to get certain data from it's database.
      The database is black box and the API could'nt be replace with using DBI from the perl script.

      Thanks for the suggestions. I'll try it out.

Re: capture error executing a binary
by Xaositect (Friar) on Jun 09, 2005 at 21:42 UTC

    I don't know for sure if the behaviour with a segfault will change this, but I don't know why it would:
    The standard way to handle unsafe code in perl is using an eval block, and then checking the error variable, like:

    eval { system('somebinary'); }; print STDERR "Arrgh!: $@\n" if ($@);