Skip to content

Fix shift/pop in (&) prototype blocks + regex \b in character class#334

Merged
fglock merged 5 commits into
masterfrom
feature/design-docs-update
Mar 19, 2026
Merged

Fix shift/pop in (&) prototype blocks + regex \b in character class#334
fglock merged 5 commits into
masterfrom
feature/design-docs-update

Conversation

@fglock
Copy link
Copy Markdown
Owner

@fglock fglock commented Mar 19, 2026

Summary

  • Bug fix 1: shift/pop without explicit @_ was defaulting to @ARGV instead of @_ when inside blocks captured via (&) prototype (e.g., Try::Tiny's catch { })
  • Bug fix 2: \b inside character class [...] caused "Unmatched (" error - now correctly converts to \x08 for Java regex compatibility
  • Design docs: Updated log4perl-compatibility.md and cpan_client.md with current status

Changes

Bug Fix 1: PrototypeArgs.java

When parsing a block for (&) prototype, the parser wasn't setting isInSubroutineBody. This caused implicit shift/pop to default to @ARGV instead of @_.

Fix: Save and set isInSubroutineBody(true) before parsing the block, restore afterward.

Bug Fix 2: RegexPreprocessorHelper.java

In Perl, \b inside [...] means backspace (0x08), but Java regex doesn't support this.

Fix: Convert \b to \x08 directly in the character class:

  • [\b] -> [\x08] - matches backspace
  • [^\b] -> [^\x08] - matches anything except backspace
  • [\b-\n] -> [\x08-\n] - preserves range semantics
  • [a\b] -> [a\x08] - matches 'a' or backspace

Impact on Try::Tiny

  • t/basic.t: Was 2/25 failing -> Now all 25 pass
  • Overall: Was 5/11 failing -> Now 4/11 failing (remaining are DESTROY-related)

Impact on JSON::PP

  • JSON::PP now loads and decodes correctly (was failing with "Unmatched (" error)

Design Doc Updates

  • log4perl-compatibility.md: Updated test results (6/73 failing, was 8/73)
  • cpan_client.md: Added Phase 9b, updated progress tracking

Test plan

  • make passes all unit tests
  • ./jcpan -t Try::Tiny shows improvement (t/basic.t now passes)
  • Manual test: sub f (&) { $_[0] } f { shift }->('test') works
  • Manual test: qr/([\b])/ compiles and matches backspace correctly
  • JSON::PP basic decode works

Generated with Devin

fglock and others added 4 commits March 19, 2026 11:32
log4perl-compatibility.md:
- Updated test results: 6/73 failing (was 8/73), 18/700 subtests (was 26/700)
- t/020Easy.t now passes all 21 tests (local $pkg::var bug fixed)
- t/051Extra.t now passes all 11 tests
- Reorganized remaining issues with clear status
- Added progress tracking section

cpan_client.md:
- Added Phase 9b: Module::Build partial support
- Updated jcpan capabilities documentation
- Added summary statistics and completed phases list
- Updated resolved questions list

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <noreply@cognition.ai>
Investigated jcpan -t Try::Tiny failures:
- Found bug: shift returns undef in blocks captured via (&) prototype
- @_ is populated correctly but shift does not work
- DESTROY-related failures are expected (finally blocks)
- caller() not reflecting set_subname is separate issue

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <noreply@cognition.ai>
When a block is captured via (&) prototype (e.g., Try::Tiny's catch),
the parser was not setting isInSubroutineBody flag. This caused implicit
shift/pop to default to @argv instead of @_.

The fix saves and sets isInSubroutineBody(true) before parsing the block,
then restores it afterward.

This fixes Try::Tiny's t/basic.t which relies on $err = shift in catch blocks.

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <noreply@cognition.ai>
Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <noreply@cognition.ai>
@fglock fglock force-pushed the feature/design-docs-update branch from 63df801 to a5c93b8 Compare March 19, 2026 10:32
In Perl, \b inside a character class [...] represents a backspace
character (0x08), but Java regex doesn't support this syntax directly.

The fix converts \b to \x08 directly in the character class:
- Remove the incorrectly pre-appended backslash from the StringBuilder
- Append \x08 (hex notation for backspace) to the pattern
- Don't double-increment offset (outer loop handles it)
- Set first=false and lastChar=0x08 for proper range validation

This approach correctly handles all cases:
- [\b] -> [\x08] - matches backspace
- [^\b] -> [^\x08] - matches anything except backspace
- [\b-\n] -> [\x08-\n] - matches range from backspace to newline
- [a\b] -> [a\x08] - matches 'a' or backspace

Fixes JSON::PP and other modules that use patterns like ([\b]).

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <noreply@cognition.ai>
@fglock fglock changed the title Fix shift/pop in (&) prototype blocks + update design docs Fix shift/pop in (&) prototype blocks + regex \b in character class Mar 19, 2026
@fglock fglock merged commit e8b1bb7 into master Mar 19, 2026
2 checks passed
@fglock fglock deleted the feature/design-docs-update branch March 19, 2026 11:35
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

Successfully merging this pull request may close these issues.

1 participant