| 1 | package App::PerlNitpick::Rule::RewriteHeredocAsQuotedString; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use String::PerlQuote qw(double_quote); | ||
| 5 | use PPI::Document; | ||
| 6 | |||
| 7 | sub rewrite { | ||
| 8 | my ($self, $doc) = @_; | ||
| 9 | my $heredocs = $doc->find('PPI::Token::HereDoc') or return; | ||
| 10 | |||
| 11 | for my $heredoc (@{ $heredocs }) { | ||
| 12 | # Only handle the heredoc that does not interpolate variables. | ||
| 13 | next unless $heredoc->content =~ /'\z/; | ||
| 14 | |||
| 15 | my $content = join '', $heredoc->heredoc; | ||
| 16 | my $tok_content = double_quote($content); | ||
| 17 | my $tok = PPI::Token::Quote::Double->new( $tok_content ); | ||
| 18 | $heredoc->insert_before($tok); | ||
| 19 | $heredoc->remove; | ||
| 20 | } | ||
| 21 | |||
| 22 | return $doc; | ||
| 23 | } | ||
| 24 | |||
| 25 | no Moose; | ||
| 26 | 1; | ||
| 27 | |||
| 28 | |||
| 29 | __END__ |