I encountered Invalid CODE attribute error quite a few times while trying to write examples for Attribute::Handlers.

In a basic case this would happen if you try to set an attribute on a function that has not been declared.

The obvious case is this, when we have not declared the Wrap attribute in the code:

examples/attributes/wrap.pl

use strict;
use warnings;
use 5.010;

sub sum :Wrap {
    my $sum = 0;
    $sum += $_ for @_;
    return $sum;
}

say sum(2, 3);

However this also happened to me when I was trying to import the attribute from the module where I declared it. The only solution I found so far is to inherit from the module (e.g. using base) as you can see in the article about Attribute::Handlers.