Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
There is another method to get the name other than pad walking or name passing. I used this method in the 11 year old CGI::Ex::Dump module (looks like the POD for that module is rather lackluster - guess I should update it after 11 years). We've used a variant of this for some time.

Whenever we debug we also like to see the line location - once we have parsed for the line location it is trivial to open the code and look for that line and do a very barebones parse. Apologist notes: This may not be academically correct, but we don't care - we're debugging. The output will also not be right if the source file changes, or the debug line is too complex - but again I don't care about a perfect solution - I want a fast good enough solution. Maybe someday we'll update it to a padwalker, but it is really rather trivial as it is. Here is a sample:

$ cat sample.pl use strict; use CGI::Ex::Dump qw(debug); my $a = 123; my @b = qw(1..10); my $obj = bless {}, __PACKAGE__; debug; debug $a, \@b, $obj; debug "Arbitrary String "x2; debug {"a".."f"}; $ perl sample.pl debug: sample.pl line 6 debug: sample.pl line 7 $a = 123; \@b = [ "1..10" ]; $obj = bless( {}, 'main' ); debug: sample.pl line 8 String = "Arbitrary String Arbitrary String "; debug: sample.pl line 9 {"a".."f"} = { a => "b", c => "d", e => "f" };
The actual dumping is done by Data::Dumper, so setting all Data::Dumper options will control the output. It should be easy enough to replace the output with Data::Dump, or Data::Dumper::Simple, or even JSON or YAML, or any of the other 100 ways to dump out data. We used Data::Dumper because 11 years ago it was what there was.

Internally we use a variation of this codebase called simply "Debug". By default debug goes to STDOUT, but we have variants called debug_warn that goes to STDERR and a debug_dlog variant that will go directly to our perl based system logger.

my @a=qw(random brilliant braindead); print $a[rand(@a)];

In reply to Re: dumping variables automatically with their name? by Rhandom
in thread dumping variables automatically with their name? by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-19 18:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found