Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/Perl/Critic/Policy/CodeLayout/RequireTidyCode.pm |
Statements | Executed 24 statements in 692µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 91.9ms | 112ms | BEGIN@18 | Perl::Critic::Policy::CodeLayout::RequireTidyCode::
1 | 1 | 1 | 16µs | 16µs | BEGIN@10 | Perl::Critic::Policy::CodeLayout::RequireTidyCode::
1 | 1 | 1 | 12µs | 420µs | BEGIN@20 | Perl::Critic::Policy::CodeLayout::RequireTidyCode::
1 | 1 | 1 | 10µs | 74µs | BEGIN@21 | Perl::Critic::Policy::CodeLayout::RequireTidyCode::
1 | 1 | 1 | 8µs | 28µs | BEGIN@16 | Perl::Critic::Policy::CodeLayout::RequireTidyCode::
1 | 1 | 1 | 7µs | 11µs | BEGIN@12 | Perl::Critic::Policy::CodeLayout::RequireTidyCode::
1 | 1 | 1 | 7µs | 388µs | BEGIN@14 | Perl::Critic::Policy::CodeLayout::RequireTidyCode::
1 | 1 | 1 | 7µs | 19µs | BEGIN@11 | Perl::Critic::Policy::CodeLayout::RequireTidyCode::
1 | 1 | 1 | 6µs | 7µs | default_severity | Perl::Critic::Policy::CodeLayout::RequireTidyCode::
1 | 1 | 1 | 5µs | 5µs | supported_parameters | Perl::Critic::Policy::CodeLayout::RequireTidyCode::
1 | 1 | 1 | 4µs | 4µs | BEGIN@15 | Perl::Critic::Policy::CodeLayout::RequireTidyCode::
0 | 0 | 0 | 0s | 0s | applies_to | Perl::Critic::Policy::CodeLayout::RequireTidyCode::
0 | 0 | 0 | 0s | 0s | default_themes | Perl::Critic::Policy::CodeLayout::RequireTidyCode::
0 | 0 | 0 | 0s | 0s | initialize_if_enabled | Perl::Critic::Policy::CodeLayout::RequireTidyCode::
0 | 0 | 0 | 0s | 0s | violates | Perl::Critic::Policy::CodeLayout::RequireTidyCode::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | ############################################################################## | ||||
2 | # $URL$ | ||||
3 | # $Date$ | ||||
4 | # $Author$ | ||||
5 | # $Revision$ | ||||
6 | ############################################################################## | ||||
7 | |||||
8 | package Perl::Critic::Policy::CodeLayout::RequireTidyCode; | ||||
9 | |||||
10 | 2 | 40µs | 1 | 16µs | # spent 16µs within Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@10 which was called:
# once (16µs+0s) by Module::Pluggable::Object::_require at line 10 # spent 16µs making 1 call to Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@10 |
11 | 2 | 20µs | 2 | 30µs | # spent 19µs (7+12) within Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@11 which was called:
# once (7µs+12µs) by Module::Pluggable::Object::_require at line 11 # spent 19µs making 1 call to Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@11
# spent 12µs making 1 call to strict::import |
12 | 2 | 24µs | 2 | 15µs | # spent 11µs (7+4) within Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@12 which was called:
# once (7µs+4µs) by Module::Pluggable::Object::_require at line 12 # spent 11µs making 1 call to Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@12
# spent 4µs making 1 call to warnings::import |
13 | |||||
14 | 2 | 24µs | 2 | 768µs | # spent 388µs (7+380) within Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@14 which was called:
# once (7µs+380µs) by Module::Pluggable::Object::_require at line 14 # spent 388µs making 1 call to Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@14
# spent 380µs making 1 call to English::import |
15 | 2 | 18µs | 1 | 4µs | # spent 4µs within Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@15 which was called:
# once (4µs+0s) by Module::Pluggable::Object::_require at line 15 # spent 4µs making 1 call to Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@15 |
16 | 2 | 20µs | 2 | 48µs | # spent 28µs (8+20) within Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@16 which was called:
# once (8µs+20µs) by Module::Pluggable::Object::_require at line 16 # spent 28µs making 1 call to Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@16
# spent 20µs making 1 call to Exporter::import |
17 | |||||
18 | 2 | 122µs | 1 | 112ms | # spent 112ms (91.9+19.7) within Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@18 which was called:
# once (91.9ms+19.7ms) by Module::Pluggable::Object::_require at line 18 # spent 112ms making 1 call to Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@18 |
19 | |||||
20 | 2 | 29µs | 2 | 828µs | # spent 420µs (12+408) within Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@20 which was called:
# once (12µs+408µs) by Module::Pluggable::Object::_require at line 20 # spent 420µs making 1 call to Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@20
# spent 408µs making 1 call to Exporter::import |
21 | 2 | 377µs | 2 | 139µs | # spent 74µs (10+65) within Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@21 which was called:
# once (10µs+65µs) by Module::Pluggable::Object::_require at line 21 # spent 74µs making 1 call to Perl::Critic::Policy::CodeLayout::RequireTidyCode::BEGIN@21
# spent 65µs making 1 call to base::import |
22 | |||||
23 | 1 | 600ns | our $VERSION = '1.121'; | ||
24 | |||||
25 | #----------------------------------------------------------------------------- | ||||
26 | |||||
27 | 1 | 2µs | 1 | 46µs | Readonly::Scalar my $DESC => q{Code is not tidy}; # spent 46µs making 1 call to Readonly::Scalar |
28 | 1 | 2µs | 1 | 55µs | Readonly::Scalar my $EXPL => [ 33 ]; # spent 55µs making 1 call to Readonly::Scalar |
29 | |||||
30 | #----------------------------------------------------------------------------- | ||||
31 | |||||
32 | # spent 5µs within Perl::Critic::Policy::CodeLayout::RequireTidyCode::supported_parameters which was called:
# once (5µs+0s) by Perl::Critic::Policy::new at line 88 of Perl/Critic/Policy.pm | ||||
33 | return ( | ||||
34 | { | ||||
35 | 1 | 8µs | name => 'perltidyrc', | ||
36 | description => 'The Perl::Tidy configuration file to use, if any.', | ||||
37 | default_string => undef, | ||||
38 | }, | ||||
39 | ); | ||||
40 | } | ||||
41 | |||||
42 | 1 | 2µs | # spent 7µs (6+1) within Perl::Critic::Policy::CodeLayout::RequireTidyCode::default_severity which was called:
# once (6µs+1µs) by Perl::Critic::Policy::get_severity at line 331 of Perl/Critic/Policy.pm | ||
43 | sub default_themes { return qw(core pbp cosmetic) } | ||||
44 | sub applies_to { return 'PPI::Document' } | ||||
45 | |||||
46 | #----------------------------------------------------------------------------- | ||||
47 | |||||
48 | sub initialize_if_enabled { | ||||
49 | my ($self, $config) = @_; | ||||
50 | |||||
51 | # Set configuration if defined | ||||
52 | if (defined $self->{_perltidyrc} && $self->{_perltidyrc} eq $EMPTY) { | ||||
53 | my $rc = $EMPTY; | ||||
54 | $self->{_perltidyrc} = \$rc; | ||||
55 | } | ||||
56 | |||||
57 | return $TRUE; | ||||
58 | } | ||||
59 | |||||
60 | #----------------------------------------------------------------------------- | ||||
61 | |||||
62 | sub violates { | ||||
63 | my ( $self, $elem, $doc ) = @_; | ||||
64 | |||||
65 | # Perl::Tidy seems to produce slightly different output, depending | ||||
66 | # on the trailing whitespace in the input. As best I can tell, | ||||
67 | # Perl::Tidy will truncate any extra trailing newlines, and if the | ||||
68 | # input has no trailing newline, then it adds one. But when you | ||||
69 | # re-run it through Perl::Tidy here, that final newline gets lost, | ||||
70 | # which causes the policy to insist that the code is not tidy. | ||||
71 | # This only occurs when Perl::Tidy is writing the output to a | ||||
72 | # scalar, but does not occur when writing to a file. I may | ||||
73 | # investigate further, but for now, this seems to do the trick. | ||||
74 | |||||
75 | my $source = $doc->serialize(); | ||||
76 | $source =~ s{ \s+ \Z}{\n}xms; | ||||
77 | |||||
78 | # Remove the shell fix code from the top of program, if applicable | ||||
79 | ## no critic (ProhibitComplexRegexes) | ||||
80 | my $shebang_re = qr< [#]! [^\015\012]+ [\015\012]+ >xms; | ||||
81 | my $shell_re = qr<eval [ ] 'exec [ ] [^\015\012]* [ ] \$0 [ ] \$[{]1[+]"\$@"}' | ||||
82 | [ \t]*[\012\015]+ [ \t]* if [^\015\012]+ [\015\012]+ >xms; | ||||
83 | $source =~ s/\A ($shebang_re) $shell_re /$1/xms; | ||||
84 | |||||
85 | my $dest = $EMPTY; | ||||
86 | my $stderr = $EMPTY; | ||||
87 | |||||
88 | |||||
89 | # Perl::Tidy gets confused if @ARGV has arguments from | ||||
90 | # another program. Also, we need to override the | ||||
91 | # stdout and stderr redirects that the user may have | ||||
92 | # configured in their .perltidyrc file. | ||||
93 | local @ARGV = qw(-nst -nse); | ||||
94 | |||||
95 | # Trap Perl::Tidy errors, just in case it dies | ||||
96 | my $eval_worked = eval { | ||||
97 | |||||
98 | # Perl::Tidy 20120619 no longer accepts a scalar reference for stdio. | ||||
99 | my $handle = IO::String->new( $stderr ); | ||||
100 | |||||
101 | # Begining with version 20120619, Perl::Tidy modifies $source. So we | ||||
102 | # make a copy so we can get a good comparison after tidying. Doing an | ||||
103 | # s/// on $source after the fact appears not to work with previous | ||||
104 | # versions of Perl::Tidy. | ||||
105 | my $source_copy = $source; | ||||
106 | |||||
107 | # In version 20120619 (and possibly earlier), Perl::Tidy assigns the | ||||
108 | # stderr parameter directly to *STDERR. So when our $stderr goes out | ||||
109 | # of scope, the handle gets closed. Subsequent calls to warn() will | ||||
110 | # then cause a fatal exception. See RT #78182 for more details. In | ||||
111 | # the meantime, we workaround it by localizing STDERR first. | ||||
112 | local *STDERR = \*STDERR; | ||||
113 | |||||
114 | Perl::Tidy::perltidy( | ||||
115 | source => \$source_copy, | ||||
116 | destination => \$dest, | ||||
117 | stderr => $handle, | ||||
118 | defined $self->{_perltidyrc} ? (perltidyrc => $self->{_perltidyrc}) : (), | ||||
119 | ); | ||||
120 | 1; | ||||
121 | }; | ||||
122 | |||||
123 | if ($stderr or not $eval_worked) { | ||||
124 | # Looks like perltidy had problems | ||||
125 | return $self->violation( 'perltidy had errors!!', $EXPL, $elem ); | ||||
126 | } | ||||
127 | |||||
128 | if ( $source ne $dest ) { | ||||
129 | return $self->violation( $DESC, $EXPL, $elem ); | ||||
130 | } | ||||
131 | |||||
132 | return; #ok! | ||||
133 | } | ||||
134 | |||||
135 | 1 | 4µs | 1; | ||
136 | |||||
137 | #----------------------------------------------------------------------------- | ||||
138 | |||||
139 | __END__ |