Question

I want to create a function which can be imported and used as a code attribute. I am using Sub::Attribute to create the attributes. The example given uses inheritance but I want to export and use it. Hope my question is clear.

Code snippet to create an attribute:

package attpkg;

use strict;
use Sub::Attribute;
use Exporter 'import';

our @EXPORT_OK = ('Myattr');

sub Myattr : ATTR_SUB {
    my ($class, $sym_ref, $code_ref, $attr_name, $attr_data) = @_;
    print "@_";
    #$code_ref->();
    print "_____________\n";
}

1;

Code Snippet that tries to import the attribute but fails:

use strict;
use attpkg qw(Myattr);

sub pHelloWorld : Myattr {
    print "Hello World\n";
}

pHelloWorld();
Was it helpful?

Solution

Sub::Attribute secretly also creates a function called MODIFY_CODE_ATTRIBUTES() in the attpkg namespace. You'll need to export that too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top