Hi all,

I'm adding a debug function to a central module in a perl project. The plan is to listen to SIGUSR1 and if receiving it dump a lot of useful info to a file.


The signal handling isn't a problem but I'm not sure how to go about with dumping the data. What I'd really want put out is:
1. All global variables in the starting script
2. The local variables of the function it got interrupted from
3. Where in the code the script currently is

3 is very straight forward with caller() and my hope is to use %:: for 1 and 2. Unfortunately I've got some problem with them, for 1 I don't know how to know what type it is (hash,scalar, array, code...), or rather I thought I knew by doing:
foreach my $entry (sort values %:: ) { print "*" x 10, "Name: $entry Type: "; print "scalar is defined\t" if defined ${$entry}; print "array is defined\t" if defined @{$entry}; print "hash is defined\t" if defined %{$entry}; print "sub is defined\t" if defined &{$entry}; print "\n"; }
This one misses variables though which really confuses me.
So I suppose my questions are:
1. How do I reliably list and print all globally declared variables in the main:: name space?
2. Is it at all possible to access the local variables of a subroutine which is currently running? (I mean the variables of the subroutine which was executing when the script got the signal)
3. If 2 is possible, can I trace upwards and get the variables for subroutines calling the "active" subroutine?

I've read through Mastering perl chap 8, perlvar, perlmod and perlref but I don't see much in traversing the symbol table.
Using CPAN modules is unfortunately not an option due to access restrictions in the target environment so I'll be forced to reinvent wheels if need be.
The small isolated code I currently have is:
use strict; use sig_bla; sub bar { my $foo = 5; open (FO, ">/tmp/test_file") or die "doh!?"; print "file open\n"; print FO "Some small data in the open file\n"; while (1) { sleep 1; } } our @MYARR = ( 1, 2 ,3); my @my_arr = ( 1, 2 ,3); my $apa = 2; my $FOO = 10; our %MY_HASH = ( a => 2, b => 5); my %myhash = ( a => 2, b => 5); print "$$\n"; &bar();
and
package sig_bla; use strict; use Data::Dumper; $SIG{USR1} = \&handle; sub handle { print "Got sig\n"; for (0..5) { my (@ap) = caller ($_); last unless scalar @ap; print Dumper \@ap; } foreach my $entry (sort values %:: ) { print "*" x 10, "Name: $entry Type: "; print "scalar is defined\t" if defined ${$entry}; print "array is defined\t" if defined @{$entry}; print "hash is defined\t" if defined %{$entry}; print "sub is defined\t" if defined &{$entry}; print "\n"; } } 1;
Thanks in advance

In reply to Dumping the content of a module by jmo

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.