Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Filtering methods in Perl debugger

by choroba (Bishop)
on Jan 24, 2018 at 11:57 UTC ( #1207812=perlmeditation: print w/replies, xml ) Need Help??

[arc444]: hi. Anyone know how to filter the list of methods returned when querying an object in the perl debugger ?
[arc444]: for example : m $obj | grep blah

Reading help in the debugger hasn't shown anything related; reading perl5db.pl confirmed such a feature didn't exist. So, the only way was to patch the debugger.

I didn't have much time, so the solution is ugly: the syntax for classes is different to the syntax for objects. For objects, you have to use strings and separate the argument by a comma, for classes, the comma is forbidden and the regex is specified directly without quotes:

$ perl -d -e 'sub foo { "bar" }; $o = bless {}, "main"' Loading DB routines from perl5db.pl version 1.39_10 Editor support available. Enter h or 'h h' for help, or 'man perldebug' for more help. main::(-e:1): sub foo { "bar" }; $o = bless {}, "main" DB<1> m $o foo via UNIVERSAL: DOES via UNIVERSAL: VERSION via UNIVERSAL: can via UNIVERSAL: isa DB<2> m $o, '(?i:o)' foo via UNIVERSAL: DOES via UNIVERSAL: VERSION DB<3> m main foo via UNIVERSAL: DOES via UNIVERSAL: VERSION via UNIVERSAL: can via UNIVERSAL: isa DB<4> m main (?i:o) foo via UNIVERSAL: DOES via UNIVERSAL: VERSION

And here's the patch:

diff --git a/lib/perl5db.pl b/lib/perl5db.pl index ecc49a814d..d50dfd22b4 100644 --- a/lib/perl5db.pl +++ b/lib/perl5db.pl @@ -767,7 +767,7 @@ sub eval { dumpit( $OUT, \@res ); } elsif ( $onetimeDump eq 'methods' ) { - methods( $res[0] ); + methods( @res ); } } ## end elsif ($onetimeDump) @res; @@ -2395,8 +2395,8 @@ sub _DB__handle_run_command_in_pager_command { sub _DB__handle_m_command { my ($obj) = @_; - if ($cmd =~ s#\Am\s+([\w:]+)\s*\z# #) { - methods($1); + if ($cmd =~ s#\Am\s+([\w:]+)\s*(.*)# #) { + methods($1, $2); next CMD; } @@ -8894,16 +8894,16 @@ sub methods { # Figure out the class - either this is the class or it's a refer +ence # to something blessed into that class. - my $class = shift; + my ($class, $filter) = @_; $class = ref $class if ref $class; local %seen; # Show the methods that this class has. - methods_via( $class, '', 1 ); + methods_via( $class, '', $filter, 1 ); # Show the methods that UNIVERSAL has. - methods_via( 'UNIVERSAL', 'UNIVERSAL', 0 ); + methods_via( 'UNIVERSAL', 'UNIVERSAL', $filter, 0 ); } ## end sub methods =head2 C<methods_via($class, $prefix, $crawl_upward)> @@ -8927,6 +8927,7 @@ sub methods_via { my $prepend = $prefix ? "via $prefix: " : ''; my @to_print; + my $filter = shift; # Extract from all the symbols in this class. my $class_ref = do { no strict "refs"; \%{$class . '::'} }; while (my ($name, $glob) = each %$class_ref) { @@ -8939,7 +8940,7 @@ sub methods_via { # \$glob will be SCALAR in both cases. if ((ref $glob || ($glob && ref \$glob eq 'GLOB' && defined & +$glob)) && !$seen{$name}++) { - push @to_print, "$prepend$name\n"; + push @to_print, "$prepend$name\n" unless $filter && $name + !~ /$filter/; } } @@ -8961,7 +8962,7 @@ sub methods_via { $prepend = $prefix ? $prefix . " -> $name" : $name; # Crawl up the tree and keep trying to crawl up. - methods_via( $name, $prepend, 1 ); + methods_via( $name, $prepend, $filter, 1 ); } } ## end sub methods_via

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1207812]
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2018-02-19 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (266 votes). Check out past polls.

    Notices?