File Coverage

blib/lib/Parse/Functions.pm
Criterion Covered Total %
statement 42 46 91.3
branch 11 20 55.0
condition 7 12 58.3
subroutine 6 6 100.0
pod 0 4 0.0
total 66 88 75.0


line stmt bran cond sub pod time code
1             package Parse::Functions;
2 6     6   23142 use strict;
  6         12  
  6         296  
3 6     6   31 use warnings;
  6         11  
  6         4434  
4              
5             our $VERSION = '0.01';
6              
7             =head1 NAME
8              
9             Parse::Functions - list all the functions in source code
10              
11             =head1 DESCRIPTION
12              
13             See L
14              
15             =cut
16              
17             sub new {
18 16     16 0 9446 my ($class) = @_;
19 16         77 my $self = bless {}, $class;
20 16         39 return $self;
21             }
22              
23             sub sort_functions {
24 15     15 0 29 my ($self, $functions, $order) = @_;
25              
26 15 100       98 return $functions if not $order;
27              
28 10         20 my @sorted;
29              
30             # Sort it appropriately
31 10 100       49 if ( $order eq 'alphabetical' ) {
    50          
32              
33             # Alphabetical (aka 'abc')
34             # Ignore case and leading non-word characters
35 5         10 my @expected = ();
36 5         11 my @unknown = ();
37 5         15 foreach my $function (@$functions) {
38 33 50       116 if ( $function =~ /^([^a-zA-Z0-9]*)(.*)$/ ) {
39 33         134 push @expected, [ $function, $1, lc($2) ];
40             } else {
41 0         0 push @unknown, $function;
42             }
43             }
44 33 50 66     73 @expected = map { $_->[0] } sort {
  59   33     206  
45 5         35 $a->[2] cmp $b->[2]
46             || length( $a->[1] ) <=> length( $b->[1] )
47             || $a->[1] cmp $b->[1]
48             || $a->[0] cmp $b->[0]
49             } @expected;
50 0 0       0 @unknown =
51 5         16 sort { lc($a) cmp lc($b) || $a cmp $b } @unknown;
52 5         17 @sorted = ( @expected, @unknown );
53              
54             } elsif ( $order eq 'alphabetical_private_last' ) {
55              
56             # As above, but with private functions last
57 5         11 my @expected = ();
58 5         12 my @unknown = ();
59 5         14 foreach my $function (@$functions) {
60 33 50       106 if ( $function =~ /^([^a-zA-Z0-9]*)(.*)$/ ) {
61 33         117 push @expected, [ $function, $1, lc($2) ];
62             } else {
63 0         0 push @unknown, $function;
64             }
65             }
66 33 50 66     69 @expected = map { $_->[0] } sort {
  62   66     488  
67 5         21 length( $a->[1] ) <=> length( $b->[1] )
68             || $a->[1] cmp $b->[1]
69             || $a->[2] cmp $b->[2]
70             || $a->[0] cmp $b->[0]
71             } @expected;
72 0 0       0 @unknown =
73 5         15 sort { lc($a) cmp lc($b) || $a cmp $b } @unknown;
74 5         17 @sorted = ( @expected, @unknown );
75              
76             }
77              
78 10         95 return \@sorted;
79             }
80              
81             # recognize newline even if encoding is not the platform default (will not work for MacOS classic)
82 15     15 0 133 sub newline { qr{\cM?\cJ} };
83              
84             sub find {
85 16     16 0 7266 my ($self, $text, $sort) = @_;
86              
87             # Show an empty function list by default
88 16 100       52 return () if not $text;
89              
90 15         63 my $function_re = $self->function_re;
91              
92 15         1154 my @functions = grep { defined $_ } $text =~ /$function_re/g;
  171         338  
93              
94 15         33 return @{ $self->sort_functions( \@functions, $sort ) };
  15         77  
95             }
96              
97              
98              
99             1;
100              
101             # Copyright 2008-2014 The Padre development team as listed in Padre.pm.
102             # LICENSE
103             # This program is free software; you can redistribute it and/or
104             # modify it under the same terms as Perl 5 itself.