← Index
NYTProf Performance Profile   « line view »
For /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/bin/perlcritic
  Run on Sat Mar 19 22:12:22 2016
Reported on Sat Mar 19 22:14:12 2016

Filename/Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/5.18.2/Text/ParseWords.pm
StatementsExecuted 15 statements in 767µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11116µs28µsText::ParseWords::::BEGIN@3Text::ParseWords::BEGIN@3
11110µs24µsText::ParseWords::::BEGIN@62Text::ParseWords::BEGIN@62
1119µs16µsText::ParseWords::::BEGIN@133Text::ParseWords::BEGIN@133
1118µs22µsText::ParseWords::::BEGIN@8Text::ParseWords::BEGIN@8
0000s0sText::ParseWords::::nested_quotewordsText::ParseWords::nested_quotewords
0000s0sText::ParseWords::::old_shellwordsText::ParseWords::old_shellwords
0000s0sText::ParseWords::::parse_lineText::ParseWords::parse_line
0000s0sText::ParseWords::::quotewordsText::ParseWords::quotewords
0000s0sText::ParseWords::::shellwordsText::ParseWords::shellwords
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Text::ParseWords;
2
3234µs240µs
# spent 28µs (16+12) within Text::ParseWords::BEGIN@3 which was called: # once (16µs+12µs) by Perl::Critic::Policy::Documentation::PodSpelling::BEGIN@22 at line 3
use strict;
# spent 28µs making 1 call to Text::ParseWords::BEGIN@3 # spent 12µs making 1 call to strict::import
418µsrequire 5.006;
51400nsour $VERSION = "3.28";
6
7
82249µs237µs
# spent 22µs (8+14) within Text::ParseWords::BEGIN@8 which was called: # once (8µs+14µs) by Perl::Critic::Policy::Documentation::PodSpelling::BEGIN@22 at line 8
use Exporter;
# spent 22µs making 1 call to Text::ParseWords::BEGIN@8 # spent 14µs making 1 call to Exporter::import
914µsour @ISA = qw(Exporter);
1011µsour @EXPORT = qw(shellwords quotewords nested_quotewords parse_line);
111300nsour @EXPORT_OK = qw(old_shellwords);
1210sour $PERL_SINGLE_QUOTE;
13
14
15sub shellwords {
16 my (@lines) = @_;
17 my @allwords;
18
19 foreach my $line (@lines) {
20 $line =~ s/^\s+//;
21 my @words = parse_line('\s+', 0, $line);
22 pop @words if (@words and !defined $words[-1]);
23 return() unless (@words || !length($line));
24 push(@allwords, @words);
25 }
26 return(@allwords);
27}
28
- -
31sub quotewords {
32 my($delim, $keep, @lines) = @_;
33 my($line, @words, @allwords);
34
35 foreach $line (@lines) {
36 @words = parse_line($delim, $keep, $line);
37 return() unless (@words || !length($line));
38 push(@allwords, @words);
39 }
40 return(@allwords);
41}
42
- -
45sub nested_quotewords {
46 my($delim, $keep, @lines) = @_;
47 my($i, @allwords);
48
49 for ($i = 0; $i < @lines; $i++) {
50 @{$allwords[$i]} = parse_line($delim, $keep, $lines[$i]);
51 return() unless (@{$allwords[$i]} || !length($lines[$i]));
52 }
53 return(@allwords);
54}
55
- -
58sub parse_line {
59 my($delimiter, $keep, $line) = @_;
60 my($word, @pieces);
61
622214µs238µs
# spent 24µs (10+14) within Text::ParseWords::BEGIN@62 which was called: # once (10µs+14µs) by Perl::Critic::Policy::Documentation::PodSpelling::BEGIN@22 at line 62
no warnings 'uninitialized'; # we will be testing undef strings
# spent 24µs making 1 call to Text::ParseWords::BEGIN@62 # spent 14µs making 1 call to warnings::unimport
63
64 while (length($line)) {
65 # This pattern is optimised to be stack conservative on older perls.
66 # Do not refactor without being careful and testing it on very long strings.
67 # See Perl bug #42980 for an example of a stack busting input.
68 $line =~ s/^
69 (?:
70 # double quoted string
71 (") # $quote
72 ((?>[^\\"]*(?:\\.[^\\"]*)*))" # $quoted
73 | # --OR--
74 # singe quoted string
75 (') # $quote
76 ((?>[^\\']*(?:\\.[^\\']*)*))' # $quoted
77 | # --OR--
78 # unquoted string
79 ( # $unquoted
80 (?:\\.|[^\\"'])*?
81 )
82 # followed by
83 ( # $delim
84 \Z(?!\n) # EOL
85 | # --OR--
86 (?-x:$delimiter) # delimiter
87 | # --OR--
88 (?!^)(?=["']) # a quote
89 )
90 )//xs or return; # extended layout
91 my ($quote, $quoted, $unquoted, $delim) = (($1 ? ($1,$2) : ($3,$4)), $5, $6);
92
93
94 return() unless( defined($quote) || length($unquoted) || length($delim));
95
96 if ($keep) {
97 $quoted = "$quote$quoted$quote";
98 }
99 else {
100 $unquoted =~ s/\\(.)/$1/sg;
101 if (defined $quote) {
102 $quoted =~ s/\\(.)/$1/sg if ($quote eq '"');
103 $quoted =~ s/\\([\\'])/$1/g if ( $PERL_SINGLE_QUOTE && $quote eq "'");
104 }
105 }
106 $word .= substr($line, 0, 0); # leave results tainted
107 $word .= defined $quote ? $quoted : $unquoted;
108
109 if (length($delim)) {
110 push(@pieces, $word);
111 push(@pieces, $delim) if ($keep eq 'delimiters');
112 undef $word;
113 }
114 if (!length($line)) {
115 push(@pieces, $word);
116 }
117 }
118 return(@pieces);
119}
120
- -
123sub old_shellwords {
124
125 # Usage:
126 # use ParseWords;
127 # @words = old_shellwords($line);
128 # or
129 # @words = old_shellwords(@lines);
130 # or
131 # @words = old_shellwords(); # defaults to $_ (and clobbers it)
132
1332251µs223µs
# spent 16µs (9+7) within Text::ParseWords::BEGIN@133 which was called: # once (9µs+7µs) by Perl::Critic::Policy::Documentation::PodSpelling::BEGIN@22 at line 133
no warnings 'uninitialized'; # we will be testing undef strings
# spent 16µs making 1 call to Text::ParseWords::BEGIN@133 # spent 7µs making 1 call to warnings::unimport
134 local *_ = \join('', @_) if @_;
135 my (@words, $snippet);
136
137 s/\A\s+//;
138 while ($_ ne '') {
139 my $field = substr($_, 0, 0); # leave results tainted
140 for (;;) {
141 if (s/\A"(([^"\\]|\\.)*)"//s) {
142 ($snippet = $1) =~ s#\\(.)#$1#sg;
143 }
144 elsif (/\A"/) {
145 require Carp;
146 Carp::carp("Unmatched double quote: $_");
147 return();
148 }
149 elsif (s/\A'(([^'\\]|\\.)*)'//s) {
150 ($snippet = $1) =~ s#\\(.)#$1#sg;
151 }
152 elsif (/\A'/) {
153 require Carp;
154 Carp::carp("Unmatched single quote: $_");
155 return();
156 }
157 elsif (s/\A\\(.?)//s) {
158 $snippet = $1;
159 }
160 elsif (s/\A([^\s\\'"]+)//) {
161 $snippet = $1;
162 }
163 else {
164 s/\A\s+//;
165 last;
166 }
167 $field .= $snippet;
168 }
169 push(@words, $field);
170 }
171 return @words;
172}
173
17414µs1;
175
176__END__