As of Perl 5.36 (at least in my environment), mob's answer no longer works as there are multiple "leading lines":
$VAR1 = '{';
$VAR2 = ' do {';
$VAR3 = ' package My::Package;';
$VAR4 = ' BEGIN {${^WARNING_BITS} = "\\x55\\x55\\x55\\x55\\x55\\x55\\x55\\x55\\x55\\x55\\x55\\x55\\x55\\x55\\x54\\x55\\x55\\x55\\x55\\x55\\x55"}';
$VAR5 = ' use strict;';
$VAR6 = ' use feature \'current_sub\', \'evalbytes\', \'fc\', \'say\', \'signatures\', \'state\', \'switch\', \'unicode_strings\', \'unicode_eval\';';
$VAR7 = ' die sprintf("Too few arguments for subroutine at %s line %d.\\n", (caller)[1, 2]) unless @_ >= 1;';
$VAR8 = ' my $class = $_[0];';
$VAR9 = ' my @args = @_[1 .. $#_]';
Here is a quick alternative which I've created which seem to work for me:
sub _get_signature ($called_sub) {
my $signature = q{};
if (
!eval {
require B::Deparse;
my ( $found_feature, $found_sig, @sig, @lines );
@lines = split( /\n/, B::Deparse->new->coderef2text( \&$called_sub ) );
foreach my $line (@lines) {
if ( $line =~ /\w*\};/ ) { # we've reach the end of the "do" block or similar
last;
}
if ( $line =~ /\w*use feature/ ) {
if ( $line =~ /signatures/ ) {
$found_feature = 1;
next;
}
last; # no signatures
}
if ( $found_feature && $line =~ /^\s*my (\W\w+) =/ ) {
push( @sig, $1 );
$found_sig = 1;
next;
}
if ($found_sig) {
last; # if we have started to find signatures and then stopped, we've reached the end of them.
}
}
if ($found_sig) {
$signature = join( q{, }, @sig );
}
1;
}
) {
croak("Unable to produce signatures due to $@");
}
return "($signature)";
}