| 1 | package CodeGen::Protection::Format::Perl; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: Safely rewrite parts of Perl documents | ||||||||||
| 4 | |||||||||||
| 5 | 5 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 5 | ▶ | use Carp 'croak'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | with qw(CodeGen::Protection::Role); | |||||||||
| 8 | |||||||||||
| 9 | 5 | our $VERSION = '0.06'; | |||||||||
| 10 | |||||||||||
| 11 | 25 | ▶ | sub _tidy { | ||||||||
|
Subroutine
_tidy: called CC=5 SCAR=18.0
| |||||||||||
| 12 | 25 | my ( $self, $code ) = @_; | |||||||||
| 13 | 25 | ▶ | return $code unless my $perltidy = $self->tidy; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 14 | 5 | require Perl::Tidy; | |||||||||
| 15 | 5 | my @perltidy; | |||||||||
| 16 | 5 | ▶ | if ( '1' ne $perltidy ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 0 | ▶ | unless ( -e $perltidy ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 0 | croak("Cannot find perltidyrc file: $perltidy"); | |||||||||
| 19 | } | ||||||||||
| 20 | 0 | @perltidy = ( perltidyrc => $perltidy ); | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | 5 | my ( $stderr, $tidied ); | |||||||||
| 24 | |||||||||||
| 25 | # need to clear @ARGV or else Perl::Tidy thinks you're trying | ||||||||||
| 26 | # to provide a filename and dies | ||||||||||
| 27 | 5 | local @ARGV; | |||||||||
| 28 | 5 | ▶ | Perl::Tidy::perltidy( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | source => \$code, | ||||||||||
| 30 | destination => \$tidied, | ||||||||||
| 31 | stderr => \$stderr, | ||||||||||
| 32 | @perltidy, | ||||||||||
| 33 | ) and die "Perl::Tidy error: $stderr"; | ||||||||||
| 34 | |||||||||||
| 35 | 5 | return $tidied; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | # For both the _start_marker_format() and the _end_marker_format(), the first | ||||||||||
| 39 | # '%s' is the version number if it's being added to the document. It's a | ||||||||||
| 40 | # version regex (_version_re()) if it's being used to match the start or end | ||||||||||
| 41 | # marker. | ||||||||||
| 42 | |||||||||||
| 43 | # The second '%s' is the md5 sum if it's being added to the document. It's a | ||||||||||
| 44 | # captured md5 regex ([0-9a-f]{32}) if it's being used to match the start or | ||||||||||
| 45 | # end marker. | ||||||||||
| 46 | |||||||||||
| 47 | 76 | ▶ | sub _start_marker_format { | ||||||||
|
Subroutine
_start_marker_format: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 76 | '#<<< %s %s. Do not touch any code between this and the end comment. Checksum: %s'; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 76 | ▶ | sub _end_marker_format { | ||||||||
|
Subroutine
_end_marker_format: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 76 | '#>>> %s %s. Do not touch any code between this and the start comment. Checksum: %s'; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 5 | 1; | |||||||||
| 56 | |||||||||||
| 57 | __END__ | ||||||||||