Skip to content

Fix Try::Tiny::catch when called as method via namespace pollution#354

Merged
fglock merged 1 commit into
masterfrom
fix/try-tiny-method-call-detection
Mar 23, 2026
Merged

Fix Try::Tiny::catch when called as method via namespace pollution#354
fglock merged 1 commit into
masterfrom
fix/try-tiny-method-call-detection

Conversation

@fglock
Copy link
Copy Markdown
Owner

@fglock fglock commented Mar 23, 2026

Summary

Fixes DateTime test t/48rt-115983.t which verifies that catch doesn't leak into DateTime's namespace.

Problem

When namespace::autoclean doesn't clean up Try::Tiny imports (as in PerlOnJava's stub), modules like DateTime retain catch in their namespace. This causes issues when indirect object syntax like:

catch { $dt->truncate(...) }

is parsed as:

$dt->truncate(...)->catch()

The result was Try::Tiny::catch being called with a DateTime object instead of a CODE ref, causing: set_subname requires a CODE reference

Solution

Added detection in Try::Tiny::catch for when it's incorrectly invoked as a method (first argument is a blessed reference instead of CODE). It now throws the expected error:

Can't locate object method "catch" via package "DateTime"

Test Plan

  • DateTime tests pass (./jcpan -t DateTime - 51 tests, 3589 subtests)
  • Unit tests pass (make)
  • Normal Try::Tiny usage still works

Generated with Devin

When namespace::autoclean doesn't clean up Try::Tiny imports (as in
PerlOnJava's stub), modules like DateTime retain 'catch' in their
namespace. This causes issues when indirect object syntax like
'catch { $dt->truncate(...) }' is parsed as '$dt->truncate(...)->catch()'.

The fix detects when catch() is incorrectly invoked as a method (first
argument is a blessed reference instead of CODE) and throws the expected
'Can't locate object method' error, making tests like DateTime's
t/48rt-115983.t pass.

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

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@fglock fglock merged commit 10cb4b6 into master Mar 23, 2026
2 checks passed
@fglock fglock deleted the fix/try-tiny-method-call-detection branch March 23, 2026 10:26
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