@@ -201,14 +201,10 @@ sub __TOKENIZER__on_char {
201
201
return undef ;
202
202
}
203
203
204
- # Define $line outside of the loop, so that if we encounter the
205
- # end of the file, we have access to the last line still.
206
- my $line ;
207
-
208
204
# Suck in the HEREDOC
209
205
$token -> {_heredoc } = [];
210
206
my $terminator = $token -> {_terminator } . " \n " ;
211
- while ( defined ($line = $t -> _get_line) ) {
207
+ while ( defined ( my $line = $t -> _get_line ) ) {
212
208
if ( $line eq $terminator ) {
213
209
# Keep the actual termination line for consistency
214
210
# when we are re-assembling the file
@@ -224,24 +220,25 @@ sub __TOKENIZER__on_char {
224
220
225
221
# End of file.
226
222
# Error: Didn't reach end of here-doc before end of file.
227
- # $line might be undef if we get NO lines.
228
- if ( defined $line and $line eq $token -> {_terminator } ) {
229
- # If the last line matches the terminator
230
- # but is missing the newline, we want to allow
231
- # it anyway (like perl itself does). In this case
232
- # perl would normally throw a warning, but we will
233
- # also ignore that as well.
234
- pop @{$token -> {_heredoc }};
235
- $token -> {_terminator_line } = $line ;
236
- } else {
237
- # The HereDoc was not properly terminated.
238
- $token -> {_terminator_line } = undef ;
239
223
240
- # Trim off the trailing whitespace
241
- if ( defined $token -> {_heredoc }-> [-1] and $t -> {source_eof_chop } ) {
242
- chop $token -> {_heredoc }-> [-1];
224
+ # If the here-doc block is not empty, look at the last line to determine if
225
+ # the here-doc terminator is missing a newline (which Perl would fail to
226
+ # compile but is easy to detect) or if the here-doc block was just not
227
+ # terminated at all (which Perl would fail to compile as well).
228
+ $token -> {_terminator_line } = undef ;
229
+ if ( @{$token -> {_heredoc }} and defined $token -> {_heredoc }[-1] ) {
230
+ # See PPI::Tokenizer, the algorithm there adds a space at the end of the
231
+ # document that we need to make sure we remove.
232
+ if ( $t -> {source_eof_chop } ) {
233
+ chop $token -> {_heredoc }[-1];
243
234
$t -> {source_eof_chop } = ' ' ;
244
235
}
236
+
237
+ # Check if the last line of the file matches the terminator without
238
+ # newline at the end. If so, remove it from the content and set it as
239
+ # the terminator line.
240
+ $token -> {_terminator_line } = pop @{$token -> {_heredoc }}
241
+ if $token -> {_heredoc }[-1] eq $token -> {_terminator };
245
242
}
246
243
247
244
# Set a hint for PPI::Document->serialize so it can
0 commit comments