Skip to content

fix: restrict Subs style to package-local subs only#193

Merged
toddr merged 1 commit intocpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-subs-inherited-dispatch
Mar 22, 2026
Merged

fix: restrict Subs style to package-local subs only#193
toddr merged 1 commit intocpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-subs-inherited-dispatch

Conversation

@toddr-bot
Copy link
Collaborator

What

Restrict the Subs style to only dispatch to subs defined directly in the target package, not inherited methods.

Why

The Subs style used UNIVERSAL::can() to find handlers, which walks the entire inheritance tree. This means XML element names like <connect/> or <cleanup/> could invoke inherited base class methods — a behavior inconsistent with the documentation ("a sub by that name in the package specified by the Pkg option") and potentially dangerous if the package inherits from a class with side-effectful methods.

How

Replaced $expat->{Pkg}->can($tag) with a direct symbol table check: defined &{"Pkg::$tag"}. This only finds subs actually defined in the package, matching the documented contract. Applied to both Start and End handlers.

Testing

  • New t/subs_inherited.t with 4 tests covering:
    • Handlers defined in target package are still called
    • Inherited methods are NOT dispatched via element names
    • UNIVERSAL methods (isa, can, VERSION) are not triggered
    • End handler (tag_) also uses direct lookup
  • Full suite: 47 files, 363 tests, all pass

🤖 Generated with Claude Code

The Subs style used UNIVERSAL::can() to look up handlers, which walks
the inheritance tree. This means XML element names could dispatch to
inherited methods (e.g. a base class's connect() or cleanup()), even
though the documentation says "a sub by that name in the package
specified by the Pkg option".

Replace can() with direct symbol table lookup (defined &{"Pkg::tag"})
so only subs actually defined in the target package are called. This
matches the documented behavior and prevents unintended method dispatch
through UNIVERSAL or parent classes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@toddr toddr marked this pull request as ready for review March 22, 2026 17:16
@toddr toddr merged commit 0a4690e into cpan-authors:main Mar 22, 2026
29 checks passed
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.

2 participants