Debian Codegraph @

bin/pmfunc

raw
1 #!/usr/bin/env perl
2 # pmfunc -- show a function
3
4 # ------ pragmas
5 use strict;
6 use warnings;
7
8 our $VERSION = '2.0';
9
10 # ------ define variables
11 my $errors = 0; # error count
12 my $file = undef; # module file path
13 my $function = undef; # function name
14 my $module = undef; # module name
15 my $ok = undef; # OK count
16
17 BEGIN { $^W = 1 }
18 BEGIN { die "usage: $0 module ...\n" unless @ARGV }
19
20 use FindBin qw($Bin);
21
22 $errors = 0;
23
24 for my $arg (@ARGV) {
25 ($module, $function) = $arg =~ /(\w.*)::(\w+)$/;
26 if (!defined($module)) {
27 print STDERR "Sorry, '$arg' is not the name of a function in a module.\n";
28 next;
29 }
30
31 $file = `$^X -S $Bin/pmpath $module`;
32 if ($?) {
33 $errors++;
34 next;
35 }
36 chomp $file;
37
38 my $found = 0;
39 my $ifh;
40 my $in_function = 0;
41 open $ifh, '<', $file or die "cannot open '$file': $!\n";
42 while (my $line = <$ifh>) {
43 $in_function = 1 if ($line =~ m/^sub\s+$function\W/msx);
44 next if !$in_function;
45
46 $found = 1;
47 print $line;
48
49 $in_function = 0 if $line =~ m/^}\s*$/msx;
50 }
51 close $ifh;
52 print STDERR "cannot find '$module::$function'\n" if !$found;
53
54 $errors++ if $? || !$found;
55 }
56
57 exit ($errors != 0);
58
59 __END__
60
61 =head1 NAME
62
63 pmfunc - cat out a function from a module
64
65 =head1 DESCRIPTION
66
67 Given a fully-qualified function, this program opens
68 up the file and attempts to display the source for
69 that function.
70
71 =head1 EXAMPLES
72
73 $ pmfunc Cwd::_perl_getcwd
74 sub _perl_getcwd
75 {
76 abs_path('.');
77 }
78
79 =head1 RESTRICTIONS
80
81 Only subroutines that are defined in the normal fashion are seen, since
82 a simple pattern-match is what does the extraction. Those loaded other
83 ways, such as via AUTOLOAD, typeglob aliasing, or in an C<eval>, will
84 all necessarily be missed.
85
86 This is mostly here for people who are too lazy to type
87
88 sed '/^sub getcwd/,/}/p' `pmpath Cwd`
89 or
90 perl -ne 'print if /^sub\s+getcwd\b/ .. /}/' `pmpath Cwd`
91
92 =head1 RESTRICTIONS
93
94 =head1 SEE ALSO
95
96 =head1 AUTHORS and COPYRIGHTS
97
98 Copyright (C) 1999 Tom Christiansen.
99
100 Copyright (C) 2006-2014, 2018 Mark Leighton Fisher.
101
102 =head1 LICENSE
103
104 This is free software; you can redistribute it and/or modify it
105 under the terms of either:
106 (a) the GNU General Public License as published by the Free
107 Software Foundation; either version 1, or (at your option) any
108 later version, or
109 (b) the Perl "Artistic License".
110 (This is the Perl 5 licensing scheme.)
111
112 Please note this is a change from the
113 original pmtools-1.00 (still available on CPAN),
114 as pmtools-1.00 were licensed only under the
115 Perl "Artistic License".