user2609370
user2609370

Reputation: 31

Disabling executing a subroutine in perl in the DEBUG mode

Is it possible to disable execution of a specific subroutine, when we are executing the script in DEBUG mode?.

Supoose, sub tryme is being called and takes quite a long time to execute, I would like to disable/skip executing the subroutine.

Thanks,

Upvotes: 3

Views: 343

Answers (3)

Eugen Konkov
Eugen Konkov

Reputation: 25133

You can check environment variable like this:

_long_function()   if $ENV{ DEBUG };

And run script like next if you want to this _long_function to execute:

DEBUG=1 ./script.pl

In normal cases the _long_function will not be called:

./script.pl

Upvotes: 0

amon
amon

Reputation: 57600

The $^P variable contains flags determining which debug mode is currently active. Therefore, we can write code that shows completely different behaviour in the debugger:

$ cat heisenbug.pl
use List::Util qw/sum/;
if ($^P) {
  print "You are in the debugger. Flags are ", unpack("b*", $^P), "\n";
} else {
  print "sum = ", sum(@ARGV), "\n";
}
$ perl heisenbug.pl 1 2 3 4 5 6 7 8 9 10
sum = 55
$ perl -d heisenbug.pl 1 2 3 4 5 6 7 8 9 10
Loading DB routines from perl5db.pl version 1.37
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

main::(-:2):        if ($^P) {
  DB<1> n
main::(-:3):          print "You are in the debugger. Flags are ", unpack("b*", $^P), "\n";
  DB<1> n
You are in the debugger. Flags are 10001100000111001010110010101100
Debugged program terminated.  Use q to quit or R to restart,
  use o inhibit_exit to avoid stopping after program termination,
  h q, h R or h o to get additional info.  
  DB<1> q
$

The variable and the meaning of the flags are documented in perlvar

Upvotes: 0

user1558455
user1558455

Reputation:

You can set a global Variable or a Command-Line Variable to set (for example) $debug = 1. Then you could specifiy your sub-calls like that:

_long_function() unless $debug == 1;

or

unless ($debug) {
    ...
}

Upvotes: 1

Related Questions