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

Hi, I'm trying to write a testscript that will print all symbols that are used in another script like this:
symdump script1.pl 5 7
This will dump the symbol table after line 5 in the script and after line 7 in the script. This far I've come up with this:
#!/usr/bin/perl use strict; use warnings; use Devel::Symdump; $| = 1; my $code; my $program=shift @ARGV; die ("usage: $0 <programname> <linenr.> [<linenr.>...]\n") unless ($pr +ogram); open (PRG, $program) or die "cant open '$program'\n"; my $symdumpcode = " \$main::obj=Devel::Symdump->rnew('ThisIsAnotherPackageAndMostLikelyWil +lNeverCollideWithAnyOtherNamespace'); print \$main::obj->as_string(); "; my $idx=0; foreach my $arg (sort {$a <=> $b} @ARGV) { while (<PRG>) { $idx++; $code .= $_; last if ($arg == $idx and $code .= $symdumpcode); } } close PRG; eval "package ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAny +OtherNamespace; $code"; print $@ if ($@);
This doesn't print any symbols for my testscripts. If I change the rnew('<longpackagename>') to rnew('main') it will print all symbols including the symbols used in Devel::Symdump. Tried using the diff method in Devel::Symdump but that does only show a diff (duh), so any packages used in my script AND in Devel::Symdump will not show up. What I could do is make 2 Symdump-objects, one of the eval-ed code, one of Devel::Symdump itself, and compare their as_string output manually (it's sorted already), but that doesn't help me understand why the above code doesn't work.

My questions:

Replies are listed 'Best First'.
Re: Devel::Symdump without Devel::Symdump interference
by broquaint (Abbot) on Apr 19, 2004 at 18:45 UTC
    Seems to work ok here. My test file looks like this
    no strict; # 1 # 2 # 3 # 4 sub foo { } # 5 @bar = \&foo; # 6 $baz = "a string"; # 7 %this = qw/une deux/; # 8 # 9 # 10
    And I get the following (unsightly) output
    arrays ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: bar functions ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: foo hashes ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: this ios packages scalars ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: BEGIN ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: bar ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: baz ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: foo ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: this unknowns arrays ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: bar functions ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: foo hashes ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: this ios packages scalars ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: BEGIN ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: bar ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: baz ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: foo ThisIsAnotherPackageAndMostLikelyWillNeverCollideWithAnyOtherN +amespace:: this
    Also, your script will always slurp the number of lines as provided by the last argument, which would seem to be unintended behaviour.

    As for alternatives, you might want to look at Data::Dumper or the much more accurate Data::Dumper::Streamer for displaying the contents of a symbol table.

    HTH

    _________
    broquaint

      I'm sorry to not have posted my test scripts, but they were full of my and strict, opposed to your no strict test script. Do you (or anybody else) know why that matters in this case? I supposed the my variables were in scope of the package-with-the-long-name?