Skip to content

Conversation

@mauke
Copy link
Contributor

@mauke mauke commented Sep 20, 2025

Previously: CORE::__CLASS__ and compile-time aliasing (as in BEGIN { *cls = \&CORE::__CLASS__; } ... cls) worked as expected, but runtime calls like &CORE::__CLASS__() and my $ref = \&CORE::__CLASS__; ... $ref->() would produce bizarre results, behaving like CORE::__FILE__ instead.

Now the latter throw a &CORE::__CLASS__ cannot be called directly error. This is perhaps not entirely satisfactory, but __CLASS__ is a bit special (it is not a true constant and only usable in methods) and erroring cleanly is better than silently returning wrong results.

Fixes #23737.

Partially subsumes #23343.


  • This set of changes requires a perldelta entry, and it is included.

Previously: CORE::__CLASS__ and compile-time aliasing (as in `BEGIN {
*cls = \&CORE::__CLASS__; } ... cls`) worked as expected, but runtime
calls like `&CORE::__CLASS__()` and `my $ref = \&CORE::__CLASS__; ...
$ref->()` would produce bizarre results, behaving like CORE::__FILE__
instead.

Now the latter throw a "&CORE::__CLASS__ cannot be called directly"
error. This is perhaps not entirely satisfactory, but __CLASS__ is a bit
special (it is not a true constant and only usable in methods) and
erroring cleanly is better than silently returning wrong results.

Fixes Perl#23737.

Assorted changes:

- Document core_prototype()'s `opnum` parameter.
- Add comment explaining what `*opnum = 0` means (and why it makes no
  sense for KEY___CLASS__).
- Don't hardcode assumptions about keyword codes in coresub_op(). Handle
  __PACKAGE__/__FILE__/__LINE__ explicitly and assert() nothing else is
  passed in. This effectively reverts commit c2f605d.
- Remove CORE::__CLASS__ from op/coreamp.t as it is no longer "ampable".
- Extend op/coresubs.t to handle more of the idiosyncrasies of
  CORE::__CLASS__.
Rearrange documentation slightly to make it clearer what you can(not) do
with CORE::chomp, CORE::chop, etc. and add CORE::__CLASS__ to the list.
coresub_op() no longer hardcodes assumptions about the numeric values of
__FILE__, __LINE__, __PACKAGE__ keyword symbols.
Copy link
Contributor

@leonerd leonerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, but I'd suggest some adjustment to the perldelta.

@mauke mauke force-pushed the fix-23737-amp-core-class branch from c7d59f7 to 053aa58 Compare September 20, 2025 14:41
@mauke mauke merged commit d143571 into Perl:blead Sep 22, 2025
33 checks passed
@mauke mauke deleted the fix-23737-amp-core-class branch September 22, 2025 02:06
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.

&CORE::__CLASS__ returns bogus value (filename)

3 participants