in reply to How can I print variable contents from the debugger non-interactively?

I am trying to debug a perl script without modifying it.

The single most powerful debug tool in Perl is "print" and Data::Dumper and its kinfolk like Data::Dump. Why is not modifying the program a requirement? Why not just add some print statements and be done with it?

During debug, I add print statements all the time with a comment tag field to make them easy to delete later (my program editor makes that easy). The "change code, recompile, execute code" cycle is so fast (if you have a good development environment) in Perl that modifying the code to debug it is very sensible.

Sometimes I will leave some debug statements in the code. These are things that I am likely to need later if there is some significant modification to the code.

use constant DEBUG =>1; ... if (DEBUG){...code}
In Perl if you set DEBUG =>0, there is no speed penalty for that if statement because Perl knows due to the constant value that the code will never be executed and it is not even compiled.

Maybe you can find all kinds of tricky debug statements to get the debugger to do what you want right now. However, you will not know that stuff years from now when you are debugging some modification. For the most part, I recommend using "print" for transient debug issues. For some code where modifications are likely to require examining some data structure, I put in in if(DEBUG).

  • Comment on Re: How can I print variable contents from the debugger non-interactively?
  • Download Code

Replies are listed 'Best First'.
Re^2: How can I print variable contents from the debugger non-interactively?
by Laurent_R (Canon) on Jul 15, 2017 at 08:39 UTC
    Hi Marshall,

    Your comment is very useful (and I upvoted it for that reason), but it does not really answer davehorner's question, which pertained to the use of the Perl debugger.

Re^2: How can I print variable contents from the debugger non-interactively?
by karlgoethebier (Abbot) on Jul 15, 2017 at 10:26 UTC
    "... Perl knows due to the constant value that the code will never be executed and it is not even compiled"

    I didn't know this. Very nice. Thanks.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re^2: How can I print variable contents from the debugger non-interactively?
by davehorner (Scribe) on Jul 17, 2017 at 14:15 UTC
    Marshall thank you for your reply, additional description for why one might want to debug the script without modifying it has been provided in the orginal question.

    Maybe you can find all kinds of tricky debug statements to get the debugger to do what you want right now. However, you will not know that stuff years from now when you are debugging some modification.

    I don't agree with this sentiment, I think learning to use the debugger and understanding how to use it to print and maybe even modify runtime behavior of the script without modification is a skill that could be useful years from now (i have a good memory and places to document the perl wisdom gleaned from this question/responses). Using print/debug statements is easy and effective, but I'm looking to further my understanding of basic perl tools and add to perl repertoire.
      Hi Dave!

      I was thinking of interactive debug commands.

      I believe that you will find in Perl, that the debugger is seldom used because its not required. I have to use a debugger (mandatory) when I write assembly code, I very seldom use a debugger for C code and almost never use a debugger for Perl code - for me, the Perl debugger is a once in a decade event. I have never required the Perl debugger to solve a code problem.

      I congratulate you on your curiosity and motivation to learn more about Perl!

      Perl is an amazingly introspective language. A Perl user function can find out things that are just not possible for a C program.

      Some useful stuff for debugging the caller stack is the Perl Caller() function.

      Some "standard" functions:

      $me = whoami(); $him = whowasi(); sub whoami { (caller(1))[3] } sub whowasi { (caller(2))[3] }
      I recommend study of the caller() function for your "debug repertoire".