| 1 |
#!/usr/bin/env perl |
| 2 |
# pmeth -- show a class's methods |
| 3 |
|
| 4 |
# ------ pragmas |
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
|
| 8 |
our $VERSION = '2.0.0'; |
| 9 |
|
| 10 |
# ------ define variables |
| 11 |
my $ancestor = undef; # ancestor |
| 12 |
my $errors = 0; # error count |
| 13 |
my %got_def = (); # got definition already |
| 14 |
my $name = undef; # symbol name |
| 15 |
|
| 16 |
BEGIN { $^W = 1 } |
| 17 |
BEGIN { die "usage: $0 module\n" unless @ARGV == 1 } |
| 18 |
|
| 19 |
$errors = 0; |
| 20 |
|
| 21 |
%got_def = (); |
| 22 |
|
| 23 |
show_methods($ARGV[0]); |
| 24 |
show_methods("UNIVERSAL", $ARGV[0]); |
| 25 |
|
| 26 |
sub show_methods { |
| 27 |
my $module = shift; |
| 28 |
my @indirect = @_; |
| 29 |
my @baseclasses = @indirect[ 0 .. ($#indirect-1) ]; |
| 30 |
eval "local \$^W = 0; require $module"; |
| 31 |
if ($@) { |
| 32 |
$@ =~ s/at \(eval.*$//; |
| 33 |
warn "$0: $@"; |
| 34 |
$errors++; |
| 35 |
return; |
| 36 |
} |
| 37 |
no strict 'refs'; |
| 38 |
foreach $name ( sort keys %{ $module . "::" } ) { |
| 39 |
use strict 'refs'; |
| 40 |
print '[constant] ' if declared($module . "::" . $name); |
| 41 |
|
| 42 |
if (defined &{ $module . "::" . $name } ) { |
| 43 |
print "[overridden] " if $got_def{$name}++; |
| 44 |
if (@indirect) { |
| 45 |
print join(" via ", $name, $module, @baseclasses), "\n"; |
| 46 |
} else { |
| 47 |
print "$name\n"; |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
if (my @parents = @{ $module . "::ISA" } ) { |
| 52 |
foreach $ancestor (@parents) { |
| 53 |
show_methods($ancestor, $module, @indirect); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
sub declared ($) { |
| 59 |
use constant 1.01; # don't omit this! |
| 60 |
my $name = shift; |
| 61 |
$name =~ s/^::/main::/; |
| 62 |
my $pkg = caller; |
| 63 |
my $full_name = $name =~ /::/ ? $name : "${pkg}::$name"; |
| 64 |
$constant::declared{$full_name}; |
| 65 |
} |
| 66 |
|
| 67 |
exit ($errors != 0); |
| 68 |
|
| 69 |
__END__ |
| 70 |
|
| 71 |
=head1 NAME |
| 72 |
|
| 73 |
pmeth - show a Perl class's methods |
| 74 |
|
| 75 |
=head1 DESCRIPTION |
| 76 |
|
| 77 |
Given a class name, print out all methods available to that class. |
| 78 |
It does this by loading in the class module, and walking its |
| 79 |
symbol table and those of its ancestor classes. A regular method |
| 80 |
call shows up simply: |
| 81 |
|
| 82 |
$ pmeth IO::Socket | grep '^con' |
| 83 |
confess |
| 84 |
configure |
| 85 |
connect |
| 86 |
connected |
| 87 |
|
| 88 |
But one that came from else where is noted with one or |
| 89 |
more "via" notations: |
| 90 |
|
| 91 |
DESTROY via IO::Handle |
| 92 |
export via Exporter via IO::Handle |
| 93 |
|
| 94 |
A base-class method that is unavailable due to being hidden by a close |
| 95 |
derived-class method by the same name (but accessible via SUPER::) |
| 96 |
is indicated by a leading "[overridden]" before it: |
| 97 |
|
| 98 |
[overridden] new via IO::Handle |
| 99 |
|
| 100 |
Constants declared via L<constant> have a leading "[constant]" added |
| 101 |
to the output, but XS C<define>'s are not yet so flagged. |
| 102 |
|
| 103 |
=head1 EXAMPLES |
| 104 |
|
| 105 |
$ pmeth IO::Socket |
| 106 |
AF_INET |
| 107 |
AF_UNIX |
| 108 |
INADDR_ANY |
| 109 |
INADDR_BROADCAST |
| 110 |
INADDR_LOOPBACK |
| 111 |
INADDR_NONE |
| 112 |
SOCK_DGRAM |
| 113 |
SOCK_RAW |
| 114 |
SOCK_STREAM |
| 115 |
accept |
| 116 |
bind |
| 117 |
carp |
| 118 |
confess |
| 119 |
configure |
| 120 |
connect |
| 121 |
connected |
| 122 |
croak |
| 123 |
getsockopt |
| 124 |
import |
| 125 |
inet_aton |
| 126 |
inet_ntoa |
| 127 |
listen |
| 128 |
new |
| 129 |
pack_sockaddr_in |
| 130 |
pack_sockaddr_un |
| 131 |
peername |
| 132 |
protocol |
| 133 |
recv |
| 134 |
register_domain |
| 135 |
send |
| 136 |
setsockopt |
| 137 |
shutdown |
| 138 |
sockaddr_in |
| 139 |
sockaddr_un |
| 140 |
sockdomain |
| 141 |
socket |
| 142 |
socketpair |
| 143 |
sockname |
| 144 |
sockopt |
| 145 |
socktype |
| 146 |
timeout |
| 147 |
unpack_sockaddr_in |
| 148 |
unpack_sockaddr_un |
| 149 |
DESTROY via IO::Handle |
| 150 |
SEEK_CUR via IO::Handle |
| 151 |
SEEK_END via IO::Handle |
| 152 |
SEEK_SET via IO::Handle |
| 153 |
_IOFBF via IO::Handle |
| 154 |
_IOLBF via IO::Handle |
| 155 |
_IONBF via IO::Handle |
| 156 |
_open_mode_string via IO::Handle |
| 157 |
autoflush via IO::Handle |
| 158 |
blocking via IO::Handle |
| 159 |
[overridden] carp via IO::Handle |
| 160 |
clearerr via IO::Handle |
| 161 |
close via IO::Handle |
| 162 |
[overridden] confess via IO::Handle |
| 163 |
constant via IO::Handle |
| 164 |
[overridden] croak via IO::Handle |
| 165 |
eof via IO::Handle |
| 166 |
error via IO::Handle |
| 167 |
fcntl via IO::Handle |
| 168 |
fdopen via IO::Handle |
| 169 |
fileno via IO::Handle |
| 170 |
flush via IO::Handle |
| 171 |
format_formfeed via IO::Handle |
| 172 |
format_line_break_characters via IO::Handle |
| 173 |
format_lines_left via IO::Handle |
| 174 |
format_lines_per_page via IO::Handle |
| 175 |
format_name via IO::Handle |
| 176 |
format_page_number via IO::Handle |
| 177 |
format_top_name via IO::Handle |
| 178 |
format_write via IO::Handle |
| 179 |
formline via IO::Handle |
| 180 |
gensym via IO::Handle |
| 181 |
getc via IO::Handle |
| 182 |
getline via IO::Handle |
| 183 |
getlines via IO::Handle |
| 184 |
gets via IO::Handle |
| 185 |
input_line_number via IO::Handle |
| 186 |
input_record_separator via IO::Handle |
| 187 |
ioctl via IO::Handle |
| 188 |
[overridden] new via IO::Handle |
| 189 |
new_from_fd via IO::Handle |
| 190 |
opened via IO::Handle |
| 191 |
output_field_separator via IO::Handle |
| 192 |
output_record_separator via IO::Handle |
| 193 |
print via IO::Handle |
| 194 |
printf via IO::Handle |
| 195 |
printflush via IO::Handle |
| 196 |
qualify via IO::Handle |
| 197 |
qualify_to_ref via IO::Handle |
| 198 |
read via IO::Handle |
| 199 |
setbuf via IO::Handle |
| 200 |
setvbuf via IO::Handle |
| 201 |
stat via IO::Handle |
| 202 |
sync via IO::Handle |
| 203 |
sysread via IO::Handle |
| 204 |
syswrite via IO::Handle |
| 205 |
truncate via IO::Handle |
| 206 |
ungensym via IO::Handle |
| 207 |
ungetc via IO::Handle |
| 208 |
untaint via IO::Handle |
| 209 |
write via IO::Handle |
| 210 |
_push_tags via Exporter via IO::Handle |
| 211 |
export via Exporter via IO::Handle |
| 212 |
export_fail via Exporter via IO::Handle |
| 213 |
export_ok_tags via Exporter via IO::Handle |
| 214 |
export_tags via Exporter via IO::Handle |
| 215 |
export_to_level via Exporter via IO::Handle |
| 216 |
[overridden] import via Exporter via IO::Handle |
| 217 |
require_version via Exporter via IO::Handle |
| 218 |
VERSION via UNIVERSAL |
| 219 |
can via UNIVERSAL |
| 220 |
[overridden] import via UNIVERSAL |
| 221 |
isa via UNIVERSAL |
| 222 |
|
| 223 |
=head1 NOTE |
| 224 |
|
| 225 |
Perl makes no distinction between functions, procedures, and methods, |
| 226 |
nor whether they are public or nominally private, nor whether a method |
| 227 |
is nominally a class method, an object method, or both. They all show up |
| 228 |
as subs in the package namespace. So if your class says C<use Carp>, you |
| 229 |
just polluted your namespace with things like croak() and confess(), which |
| 230 |
will appear to be available as method calls on objects of your class. |
| 231 |
|
| 232 |
=head1 SEE ALSO |
| 233 |
|
| 234 |
perltoot(1), perlobj(1) |
| 235 |
|
| 236 |
=head1 AUTHORS and COPYRIGHTS |
| 237 |
|
| 238 |
Copyright (C) 1999 Tom Christiansen. |
| 239 |
|
| 240 |
Copyright (C) 2006-2014 Mark Leighton Fisher. |
| 241 |
|
| 242 |
=head1 LICENSE |
| 243 |
|
| 244 |
This is free software; you can redistribute it and/or modify it |
| 245 |
under the terms of either: |
| 246 |
(a) the GNU General Public License as published by the Free |
| 247 |
Software Foundation; either version 1, or (at your option) any |
| 248 |
later version, or |
| 249 |
(b) the Perl "Artistic License". |
| 250 |
(This is the Perl 5 licensing scheme.) |
| 251 |
|
| 252 |
Please note this is a change from the |
| 253 |
original pmtools-1.00 (still available on CPAN), |
| 254 |
as pmtools-1.00 were licensed only under the |
| 255 |
Perl "Artistic License". |