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

I'd like to conditionally start the perl debugger with ./script.pl -d , is this possible?

I know "perl -d ./script" will do it, I'd just like to know if there's a run-time way.

Related work: Breaking into debugger, Automatically "continue" script under the debugger

Replies are listed 'Best First'.
Re: perl -d at runtime?
by davido (Cardinal) on Dec 19, 2003 at 01:38 UTC
    You could write a wrapper like this:

    #!/usr/bin/perl # Wrapper setup code goes here... # ..... if ( $condition_met ) { exec('perl -d scriptname') or die $!; } else { exec('perl scriptname') or die $!; }


    Dave

      So there's no way within Perl?

      (Just curiousity, not stubbornness)

        I researched your question twice -- once when I posted my initial reply, and again to double-check myself in response to your followup requesting another way.

        I am going out on a limb here, because even though I've researched it a bit, it's always dangerous to say, "No, there's no other way." Rather than to say that, I'll mention my gut feeling after having re-read perldebug, perldebtut and perlrun.

        What I kind of picked up on in perldebug was this line:

        ...the -d flag tells the compiler to insert source information into the parse trees it's about to hand off to the interpreter.

        So we can first understand that without the -d flag, the compiler doesn't insert into the parse trees the source info necessary to implement the debugger. That means that the script must at least be invoked with the -d flag so that the compiletime work is done that will enable the debugger to work.

        Now that we know that the -d flag must be present, the only question is, is there a way to invoke the debugger at compiletime but not have the interpreter invoke it except on demand? Re-reading perldebug and perlrun, I just don't see a way of doing that. But I'm definately not a debugger expert, and it's possible that someone more familiar with using the debugger will know what the trick is. My intuition is that there isn't a pure-Perl solution to your need though.

        However, all may not be lost. Look at the Devel:: heirarchy on CPAN. There is a wide range of development tools there, and some allow for runtime control. You haven't told us what it is that you need from the debugger, but I'm just suggesting that maybe you can satisfy the need with a tool other than the debugger; perhaps a Devel:: module.

        I hope this helps. I've done more reading on the subject than I initially intended to, but it was an interesting question.


        Dave

        If davido knew of a different way, he would've suggested it. His way is doing it within perl.
Re: perl -d at runtime?
by ccarden (Monk) on Dec 19, 2003 at 19:09 UTC
    You can definitely do this in your script:

    #!/usr/bin/perl -d

    ... loads up the debugger correctly.

    So what if you had two versions of your script ... one with -d in the shebang line, and one without? Call the -d version when your condition requires it. Sloppy but it works.

      Reworking davido's idea above:
      if ( ( do_I_want_debug ) and ( not defined( $DB::VERSION ) ) # already in debug? { exec('perl -d $0 @ARGV') or die $!; }
      I can't remember the variable to check if the debugger is running. $DEBUGGING used to work, but not here on ActiveState 5.8.0.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        I ended up stripping the -d arg and using exec.
        #!/usr/bin/perl -w BEGIN { # handle -d by itself to start the debugger @ARGV = grep { $_ eq '-d' ? !($opt_d=1) : 1 } @ARGV; exec('/usr/bin/perl','-dS',$0,@ARGV) if $opt_d; } ...
        Note that this doesn't do clustering like smarter switch process.

        Also, I don't know if I need that -S but it's not hurting yet.