I’ve never actually used subroutine attributes in perl before, but it happens that I’ve found a use for them, so I’ve been trying to understand how I can use them. In particular, I need to be able to find out, at runtime, what attributes a subroutine has. I’ve got the following test code, but it outputs only an empty list:
use attributes;
sub MODIFY_CODE_ATTRIBUTES {
my ($class,$code,@attrs) = @_;
my $allowed = 'takeshash';
my @bad = grep { $_ ne $allowed } @attrs;
return @bad;
}
sub mylog : takeshash {
my $params = shift;
my $val = log($params->{'n'}) / log($params->{'base'});
return $val;
}
use Data::Dumper;
print Dumper [attributes::get(\&mylog)];
Running perl v5.12.4, I get:
$ perl temp.pl
$VAR1 = [];
Looks like your problem is explained by the section on Available Subroutines in the attributes documentation. The explanation for the
getsubroutine says:Note the final sentence, where I have added emphasis. Looks like you need to add a FETCH_CODE_ATTRIBUTES subroutine.