Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Heredocs are stripped from stringified Document object #288

Open
h3xx opened this issue Aug 23, 2023 · 1 comment
Open

Heredocs are stripped from stringified Document object #288

h3xx opened this issue Aug 23, 2023 · 1 comment

Comments

@h3xx
Copy link
Contributor

h3xx commented Aug 23, 2023

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'.

@oalders
Copy link
Collaborator

oalders commented Aug 23, 2023

I encountered the same issue in perlimports:

https://metacpan.org/release/OALDERS/App-perlimports-0.000052/source/lib/App/perlimports/Document.pm#L1004-1006

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants