Haiyuan Zhang
Haiyuan Zhang

Reputation: 42852

How to debug perl within a bash wrapper

I'm debugging a Perl program started by a bash script. Because the Perl script consist of a huge number of Perl modules and needs to provide extremely complex options, so the bash wrapper here is definitely necessary.

Due to that limitation, in my case I can't use Perl debugger, which is the easiest way I guess. Then I turn to the old good printf. But, even though I add printfs from one to another places in different modules, nothing actually be printed out to the terminal where I start the bash wrapper.

So, I'd like you to first explain why I can't get any print info from the inside Perl scripts and how to solve my problem in this case to debug the Perl program.

Upvotes: 0

Views: 841

Answers (2)

Zaid
Zaid

Reputation: 37156

Quick Bash Hack

It should be possible to redirect STDOUT and STDERR to file:

# Inside the bash script
perl myScript.pl 1>perl_log.out 2>perl_log.err

Quick Perl Hack

Roll a sub to print debug output:

use constant debug_flag => 1; # At top of script(s)

{  my $debug_handle;

    sub debug {

        return unless debug_active;  # Only prints debug msgs if debug active

        unless ( $debug_handle ) {   # Initialize debug log

            open $debug_handle, '>', 'perl_log.out' or die $!;
        }

        print $debug_handle @_, "\n";
    }
}

# call with 'debug'

debug ( 'Array length : ', scalar @array );

Perl Best Practices

use Log::Log4perl;  # Wonderful logging module

and

use Carp;
local $SIG{__DIE__} = \&Carp::confess;  # Print out stack traces...
local $SIG{__WARN__} = \&Carp::cluck;   # ... when using die and warn

Qualm

It's better to avoid printf whenever possible and use print instead.

Upvotes: 0

Quentin
Quentin

Reputation: 944530

explain why I can't get any print info from the inside perl scripts

Presumably your bash script is swallowing the output.

Figure out what the bash is doing, and call perl directly with the same arguments and environment variables.

Upvotes: 4

Related Questions