Filename | /Users/ap13/perl5/lib/perl5/File/Grep.pm |
Statements | Executed 15 statements in 926µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 23µs | 138µs | BEGIN@10 | File::Grep::
1 | 1 | 1 | 21µs | 24µs | BEGIN@8 | File::Grep::
1 | 1 | 1 | 17µs | 39µs | BEGIN@5 | File::Grep::
1 | 1 | 1 | 10µs | 45µs | BEGIN@6 | File::Grep::
1 | 1 | 1 | 6µs | 6µs | BEGIN@9 | File::Grep::
1 | 1 | 1 | 4µs | 4µs | CORE:match (opcode) | File::Grep::
0 | 0 | 0 | 0s | 0s | __ANON__[:100] | File::Grep::
0 | 0 | 0 | 0s | 0s | __ANON__[:113] | File::Grep::
0 | 0 | 0 | 0s | 0s | __ANON__[:129] | File::Grep::
0 | 0 | 0 | 0s | 0s | __ANON__[:143] | File::Grep::
0 | 0 | 0 | 0s | 0s | __ANON__[:157] | File::Grep::
0 | 0 | 0 | 0s | 0s | __ANON__[:169] | File::Grep::
0 | 0 | 0 | 0s | 0s | __ANON__[:88] | File::Grep::
0 | 0 | 0 | 0s | 0s | _fgrep_process | File::Grep::
0 | 0 | 0 | 0s | 0s | fdo | File::Grep::
0 | 0 | 0 | 0s | 0s | fgrep | File::Grep::
0 | 0 | 0 | 0s | 0s | fgrep_flat | File::Grep::
0 | 0 | 0 | 0s | 0s | fgrep_into | File::Grep::
0 | 0 | 0 | 0s | 0s | fmap | File::Grep::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | #!/usr/bin/perl -w | ||||
2 | |||||
3 | package File::Grep; | ||||
4 | |||||
5 | 2 | 26µs | 2 | 62µs | # spent 39µs (17+23) within File::Grep::BEGIN@5 which was called:
# once (17µs+23µs) by Bio::Roary::AnnotateGroups::BEGIN@25 at line 5 # spent 39µs making 1 call to File::Grep::BEGIN@5
# spent 23µs making 1 call to strict::import |
6 | 2 | 40µs | 2 | 80µs | # spent 45µs (10+35) within File::Grep::BEGIN@6 which was called:
# once (10µs+35µs) by Bio::Roary::AnnotateGroups::BEGIN@25 at line 6 # spent 45µs making 1 call to File::Grep::BEGIN@6
# spent 35µs making 1 call to Exporter::import |
7 | |||||
8 | # spent 24µs (21+4) within File::Grep::BEGIN@8 which was called:
# once (21µs+4µs) by Bio::Roary::AnnotateGroups::BEGIN@25 at line 16 | ||||
9 | 2 | 37µs | 1 | 6µs | # spent 6µs within File::Grep::BEGIN@9 which was called:
# once (6µs+0s) by Bio::Roary::AnnotateGroups::BEGIN@25 at line 9 # spent 6µs making 1 call to File::Grep::BEGIN@9 |
10 | 2 | 90µs | 2 | 253µs | # spent 138µs (23+115) within File::Grep::BEGIN@10 which was called:
# once (23µs+115µs) by Bio::Roary::AnnotateGroups::BEGIN@25 at line 10 # spent 138µs making 1 call to File::Grep::BEGIN@10
# spent 115µs making 1 call to vars::import |
11 | 1 | 13µs | 1 | 4µs | $VERSION = sprintf( "%d.%02d", q( $Revision: 0.02 $ ) =~ /\s(\d+)\.(\d+)/ ); # spent 4µs making 1 call to File::Grep::CORE:match |
12 | 1 | 6µs | @ISA = qw(Exporter); | ||
13 | 1 | 300ns | @EXPORT = qw(); | ||
14 | 1 | 800ns | @EXPORT_OK = qw( fgrep fmap fdo ); | ||
15 | 1 | 4µs | %EXPORT_TAGS = ( ); | ||
16 | 1 | 706µs | 1 | 24µs | } # spent 24µs making 1 call to File::Grep::BEGIN@8 |
17 | |||||
18 | # Remain silent on bad files, else shoutout. | ||||
19 | 1 | 400ns | our $SILENT = 1; | ||
20 | |||||
21 | # Internal function; does the actual walk through the files, and calls | ||||
22 | # out to the coderef to do the work for each line. This gives me a bit | ||||
23 | # more flexibility with the end interface | ||||
24 | |||||
25 | sub _fgrep_process { | ||||
26 | my ( $closure, @files ) = @_; | ||||
27 | my $openfile = 0; | ||||
28 | my $abort = 0; | ||||
29 | my $i = 0; | ||||
30 | foreach my $file ( @files ) { | ||||
31 | my $fh; | ||||
32 | if ( UNIVERSAL::isa( \$file, "SCALAR" ) ) { | ||||
33 | # If it's a scalar, assume it's a file and open it | ||||
34 | open FILE, "$file" or | ||||
35 | ( !$SILENT and carp "Cannot open file '$file' for fgrep: $!" ) | ||||
36 | and next; | ||||
37 | $fh = \*FILE; | ||||
38 | $openfile = 1; | ||||
39 | } else { | ||||
40 | # Otherwise, we will assume it's a legit filehandle. | ||||
41 | # If something's | ||||
42 | # amiss, we'll catch it at <> below. | ||||
43 | $fh = $file; | ||||
44 | $openfile = 0; | ||||
45 | } | ||||
46 | my $line; | ||||
47 | eval { $line = <$fh> }; | ||||
48 | # Fix for perl5.8 - thanks to Benjamin Kram | ||||
49 | if ( $@ ) { | ||||
50 | !$SILENT and carp "Cannot use file '$file' for fgrep: $@"; | ||||
51 | last; | ||||
52 | } else { | ||||
53 | while ( defined( $line ) ) { | ||||
54 | my $state = &$closure( $i, $., $line ); | ||||
55 | if ( $state < 0 ) { | ||||
56 | # If need to shut down whole process... | ||||
57 | $abort = 1; | ||||
58 | last; # while! | ||||
59 | } elsif ( $state == 0 ) { | ||||
60 | # If need to shut down just this file... | ||||
61 | $abort = 0; | ||||
62 | last; # while! | ||||
63 | } | ||||
64 | $line = <$fh>; | ||||
65 | } | ||||
66 | } | ||||
67 | if ( $openfile ) { close $fh; } | ||||
68 | last if ( $abort ); # Fileloop... | ||||
69 | $i++; # Increment counter | ||||
70 | } | ||||
71 | return; | ||||
72 | } | ||||
73 | |||||
74 | sub fgrep (&@) { | ||||
75 | my ( $coderef, @files ) = @_; | ||||
76 | if ( wantarray ) { | ||||
77 | my @matches = map { { filename => $_, | ||||
78 | count => 0, | ||||
79 | matches => { } } } @files; | ||||
80 | my $sub = sub { | ||||
81 | my ( $file, $pos, $line ) = @_; | ||||
82 | local $_ = $line; | ||||
83 | if ( &$coderef( $file, $pos, $_ ) ) { | ||||
84 | $matches[$file]->{ count }++; | ||||
85 | $matches[$file]->{ matches }->{ $pos } = $line; | ||||
86 | } | ||||
87 | return 1; | ||||
88 | }; | ||||
89 | |||||
90 | _fgrep_process( $sub, @files ); | ||||
91 | return @matches; | ||||
92 | |||||
93 | } elsif ( defined( wantarray ) ) { | ||||
94 | my $count = 0; | ||||
95 | my $sub = sub { | ||||
96 | my ( $file, $pos, $line ) = @_; | ||||
97 | local $_ = $line; | ||||
98 | if ( &$coderef( $file, $pos, $_ ) ) { $count++ }; | ||||
99 | return 1; | ||||
100 | }; | ||||
101 | |||||
102 | _fgrep_process( $sub, @files ); | ||||
103 | return $count; | ||||
104 | } else { | ||||
105 | my $found = 0; | ||||
106 | my $sub = sub { | ||||
107 | my ( $file, $pos, $line ) = @_; | ||||
108 | local $_ = $line; | ||||
109 | if ( &$coderef( $file, $pos, $_ ) ) | ||||
110 | { $found=1; return -1; } | ||||
111 | else | ||||
112 | { return 1; } | ||||
113 | }; | ||||
114 | _fgrep_process( $sub, @files ); | ||||
115 | return $found; | ||||
116 | } | ||||
117 | } | ||||
118 | |||||
119 | sub fgrep_flat (&@) { | ||||
120 | my ( $coderef, @files ) = @_; | ||||
121 | my @matches; | ||||
122 | my $sub = sub { | ||||
123 | my ( $file, $pos, $line ) = @_; | ||||
124 | local $_ = $line; | ||||
125 | if ( &$coderef( $file, $pos, $_ ) ) { | ||||
126 | push @matches, $line; | ||||
127 | return 1; | ||||
128 | } | ||||
129 | }; | ||||
130 | _fgrep_process( $sub, @files ); | ||||
131 | return @matches; | ||||
132 | } | ||||
133 | |||||
134 | sub fgrep_into ( &$@ ) { | ||||
135 | my ( $coderef, $arrayref, @files ) = @_; | ||||
136 | my $sub = sub { | ||||
137 | my ( $file, $pos, $line ) = @_; | ||||
138 | local $_ = $line; | ||||
139 | if ( &$coderef( $file, $pos, $_ ) ) { | ||||
140 | push @$arrayref, $line; | ||||
141 | return 1; | ||||
142 | } | ||||
143 | }; | ||||
144 | _fgrep_process( $sub, @files ); | ||||
145 | return $arrayref; | ||||
146 | } | ||||
147 | |||||
148 | sub fmap (&@) { | ||||
149 | my ( $mapper, @files ) = @_; | ||||
150 | |||||
151 | my @mapped; | ||||
152 | my $sub = sub { | ||||
153 | my ( $file, $pos, $line ) = @_; | ||||
154 | local $_ = $line; | ||||
155 | push @mapped, &$mapper( $file, $pos, $_ ); | ||||
156 | return 1; | ||||
157 | }; | ||||
158 | _fgrep_process( $sub, @files ); | ||||
159 | return @mapped; | ||||
160 | } | ||||
161 | |||||
162 | sub fdo (&@) { | ||||
163 | my ( $doer, @files ) = @_; | ||||
164 | my $sub = sub { | ||||
165 | my ( $file, $pos, $line ) = @_; | ||||
166 | local $_ = $line; | ||||
167 | &$doer( $file, $pos, $_ ); | ||||
168 | return 1; | ||||
169 | }; | ||||
170 | _fgrep_process( $sub, @files ); | ||||
171 | } | ||||
172 | |||||
173 | 1 | 3µs | 1; | ||
174 | __END__ | ||||
# spent 4µs within File::Grep::CORE:match which was called:
# once (4µs+0s) by File::Grep::BEGIN@8 at line 11 |