Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.85k stars 527 forks source link

'use signatures' thinks symbolic refs use @_ #22177

Closed jimav closed 2 weeks ago

jimav commented 2 weeks ago

With use feature 'signatures';, if a signatured sub body calls a non-signatured sub using a symbolic reference with &{"..."}, the "Implicit use of @_ in subroutine entry with signatured subroutine is experimental" warning is triggered.

This seems inappropriate because no reference to $_[x] or @_ is involved. The warning happens even if no parameters are referenced in any way.

Steps to Reproduce

#!/usr/bin/env perl
use strict; use warnings; use feature 'signatures';

use constant X_MAX => 42;

sub display($val, $tname) {
  no strict 'refs';
  my $max = &{"X_MAX"}; # <--- triggers warning
  #my $max = &{"${tname}_MAX"};
  #print "$tname = $val", ($val > $max ? "(OVERSIZE)" : ""), "\n";
}
display(11, "X");
display(99, "X");

Result

Implicit use of @_ in subroutine entry with signatured subroutine is experimental at /tmp/t1 line 8.

Expected behavior

The warning doesn't seem correct.

Perl configuration perl 5.38.2 perl_dashV.txt

mauke commented 2 weeks ago

https://perldoc.perl.org/perlsub

&NAME;          # Makes current @_ visible to called subroutine.

...

If a subroutine is called using the & form, the argument list is optional, and if omitted, no @_ array is set up for the subroutine: the @_ array at the time of the call is visible to subroutine instead.

...

&foo;               # foo() get current args, like foo(@_) !!
jimav commented 2 weeks ago

Ah, of course. Changing the line to my $max = &{"${tname}_MAX"}(); resolves the problem.