fix: add encoding parameter to PPI::Document->new#438
Draft
toddr-bot wants to merge 2 commits into
Draft
Conversation
Add t/30_encoding.t with tests for the new encoding parameter: - Loading UTF-8 files with encoding => 'UTF-8' - PPI::Document::File encoding support - Wide character identifiers in UTF-8 files - Round-trip preservation of UTF-8 bytes - encoding accessor method - Backward compatibility without encoding parameter Tests are marked with $TODO as the feature is not yet implemented. Three tests properly fail: character-level length check, wide-char identifier parsing, and encoding accessor existence. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
PPI::Document->new($filename) has always read files as raw bytes with no encoding layers, making it impossible to correctly parse UTF-8 encoded Perl source files containing non-ASCII characters. Add an encoding parameter that causes the file to be read with the specified encoding layer: my $doc = PPI::Document->new($file, encoding => 'UTF-8'); Changes: - PPI::Util::_slurp() accepts optional encoding parameter, applies binmode with :encoding() layer when specified - PPI::Document->new() handles encoding by always using the slurp + lex_source path when encoding is given, ensuring the decoded string reaches the tokenizer - PPI::Document->encoding() accessor returns the encoding value - PPI::Document->save() uses the document's encoding when writing, so round-trip load/save preserves the correct byte representation - PPI::Document::_setattr() stores the encoding attribute - Remove $TODO markers from t/30_encoding.t tests Backward compatible: existing code without encoding parameter is completely unchanged. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This was referenced Apr 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PPI::Document->new($filename)reads files as raw bytes with no encoding support, making it impossible to correctly parse UTF-8 encoded Perl source files containing non-ASCII characters. This adds anencodingparameter so users can specify the file encoding:Fixes #22
Changes
PPI::Util::_slurp()accepts optionalencodingparameter, applies:encoding()layer viabinmodewhen specifiedPPI::Document->new()handles theencodingattribute by slurping with the specified encoding and passing the decoded string to the lexerPPI::Document->encoding()accessor returns the encoding the document was created withPPI::Document->save()applies the document's encoding when writing, so round-trip load/save preserves the correct byte representation on diskPPI::Document::_setattr()stores the encoding attributeTest plan
t/30_encoding.twith 15 tests covering:encoding => 'UTF-8'and verifying character-level (not byte-level) token contentPPI::Document::Fileencoding passthroughencodingaccessor methodGenerated by Kōan /fix
Quality Report
Changes: 3 files changed, 206 insertions(+), 4 deletions(-)
Code scan: clean
Tests: skipped
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline