Skip to content

Heredocs are stripped from stringified Document object #288

Open
@h3xx

Description

@h3xx

Problem description

Stringifying the PPI::Document object (same as calling ->content() on it) strips out the content of heredocs, giving back an un-parseable document.

Example code:

#!/usr/bin/perl
use strict;
use warnings;
require PPI;
my $content = "my \$heredoc = <<~HERE;\n\tLine 1\n\n\tLine 3\n\tHERE\n";
my $doc = PPI::Document->new(\$content);
print $doc;

Which prints:

my $heredoc = <<~HERE;

Expected to print:

my $heredoc = <<~HERE;
        Line 1

        Line 3
        HERE

From what I've seen, the other tokens in the code are preserved, it's just heredocs that I found were affected.

  • PPI version: 93d6cb2
  • Perl version: v5.38.0

Workaround

A workaround I've found is to call $doc->serialize instead. But this begs the question as to why the object stringifies at all if it produces broken code.

Notes

I investigated, and apparently the PPI::Token::HereDoc object only sets ->{content} to the first line. It should set it to the entire heredoc contents to fix the issue.

Possible other fix is to do use overload '""' => 'serialize' instead of => 'content'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions