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

I am a newcomer to Perl from Python. I am trying to debug an API call's Perl code, which is run via uWSGI. Currently, I'm using logs and `say`s to debug my code, but ideally, I would like to invoke the debugger at arbitrary locations in the code. In Python, I can do,
import pdb pdb.set_trace()
And the debugger will be invoked, pausing the script at the location of the set_trace() call. I'm sure there's an equivalent in Perl, but I'm just not able to find it. Please help :)

Replies are listed 'Best First'.
Re: Stopping script execution from within the code like Python's pdb
by Corion (Patriarch) on Jul 03, 2015 at 13:30 UTC

    I vastly prefer the printf / say style of debugging, but if you launch your program with

    perl -d

    you can stop it at an arbitrary place with

    $DB::single = 1;
Re: Stopping script execution from within the code like Python's pdb
by Athanasius (Cardinal) on Jul 03, 2015 at 14:04 UTC

    Hello schrodinger, and welcome to the Monastery!

    You should also look at the module Pry, by the Monastery’s own tobyink. From the documentation:

    This module gives you a function called pry that you can drop into your code anywhere. When Perl executes that line of code, it will stop and drop you into a REPL. You can use the REPL to inspect any lexical variables (and even alter them), call functions and methods, and so on.
    ...
    Once you've finished using the REPL, just hit Ctrl+D and your code will resume execution.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Stopping script execution from within the code like Python's pdb
by choroba (Cardinal) on Jul 03, 2015 at 14:09 UTC
    The tool to call the debugger from a running code is called Enbugger.
    require Enbugger; Enbugger->stop;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Stopping script execution from within the code like Python's pdb
by QM (Parson) on Jul 03, 2015 at 14:10 UTC
    While Corion has answered the primary question, I have very rarely found it necessary to do this. Rather, just loading the script in the debugger and running it from there is usually the way to go.

    perl -d script.pl some arguments here

    Only if the script is very CPU intensive, and the normal debugger slowdown (~10x in my experience) is untenable, would I resort to $DB::Single.

    Is there a reason you don't want to use the normal breakpoint features in Perl's debugger?

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