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

Hi,

I'm curious if there is a way to print all the last known contents of the variables (scalar, array, hash and reference) inside a script automatically.

So, that at the end of the script, I can just run something like print_all_variables, and it will print the contents of all variables similar to the format output of Data::Dumper.

Thanks in advance.

Replies are listed 'Best First'.
Re: How to Print
by Anonymous Monk on Jul 19, 2009 at 02:10 UTC
Re: How to Print All Variables in a Script
by graff (Chancellor) on Jul 19, 2009 at 21:16 UTC
    So, that at the end of the script, I can just run something like print_all_variables, and it will print the contents of all variables...

    Stating the goal that way only makes sense if all the variables are global in scope. And if that's true, it's hard to say whether just printing out all their values at the end is going to be much help as a debugging strategy.

    In general, it is considered better practice for relatively few variables to have global scope (apart from the ones defined by perl itself, such as ($_, $/, $\, $|, $!, $@, @INC, ...) which is a pretty long list by itself).

    And when you try to maximize the use of limited lexical scope for your own variables (which is a Good Thing™), it means many of them are not available for inspection when the script reaches its last statement before ending (they've gone out of scope, and no longer exist).

    If you're trying to debug or comprehend some spaghetti code, I don't think there is any substitute for focused, step-wise diagnosis. If it's producing bad output or you just want to know how a certain output came to be, trace back from the point where that output is printed, use 'perl -d' to run the script so you can set breakpoints, etc.