How to get trace output only from the main Perl program package

I would like to track my Perl script, but only the code in the main package and redirect the output to a file.

When you run the script with perl -d script.pl it goes into interactive mode.

I tried

 perl -d:Trace perl_05.pl 2&> output.log 

But it also keeps track of all the routines and modules that I don't need.

I am looking for something like bash set -o xtrace or sh -x .

+6
source share
2 answers

The Track your Perl program article discusses your problems regarding the number of traces and shows how you can configure the output to just keep track of what you want.

You can create your own ejection module to accurately track what you want. Since the current directory is usually already located at @INC , you can create Devel/MyTrace.pm .

Read it all to find out how the author changes the default trace function behavior to output the first traces to a file, then to STDERR, and finally restricts the output to just trace the main file.

You can take one more step to exclude all code except the main file:

 use v5.10; use autodie; BEGIN { my $trace_file = $ENV{TRACE_FILE} // "mytrace.$$"; print STDERR "Saving trace to $trace_file\n"; my $fh = do { if( $trace_file eq '-' ) { \*STDOUT } elsif( $trace_file eq 'STDERR' ) { \*STDERR } else { open my $fh, '>>', $trace_file; $fh; } }; sub DB::DB { my( $package, $file, $line ) = caller; return unless $file eq $0; my $code = \@{"::_<$file"}; print $fh "[@{[time]}] $file $l $code->[$line]"; } } 1; 

This last piece of code is one that you are particularly interested in. He does exactly what you want.

+6
source

Using perl with Devel::DumpTrace very similar to using bash -x . Like bash -x , Devel::DumpTrace expands and displays the values ​​of variables to give you an idea of ​​what your script is doing, and not just where it does it.

It also has the feature you're looking for: to enable and disable tracking on specific packages. For your use case, you will run it as

 perl -d:DumpTrace=-.*,+main my_script.pl 

or

 perl -d:DumpTrace=-.* my_script.pl 

-.* means "exclude from the trace all packets that match /^.*$/ " that is, all packets. +main means "include main trace in trace".

The default output can be quite verbose. If you want less output than this, you can specify quiet mode:

 perl -d:DumpTrace=quiet,-.*,+main my_script.pl 

(I am the author of Devel::DumpTrace )

+5
source

All Articles