The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Fukurama::Class::AttributesHandler - Helper class to provide corrrect handling of attributes

VERSION

Version 0.01 (beta)

SYNOPSIS

 {
        package MyAttributeHandler;
        sub MyAttribute {
                my $class = $_[0];
                my $method_data = $_[1];
                
                warn("Method '$method_data->{'sub_name'}' was resolved at compiletime with data: '$method_data->{'data'}'");
                # says: Method 'my_own_method' was resolved at compiletime with data: 'foo, bar'
        }
 }
 {
        package MyClass;
        use Fukurama::Class::AttributesHandler();
        Fukurama::Class::AttributesHandler->register_attributes('MyAttributeHandler');
        Fukurama::Class::AttributesHandler->export('MyClass');
        
        sub my_own_method : MyAttribute(foo, bar) {}
 }

DESCRIPTION

This module enables the possibility to define your own subroutine-attributes. This is also done with the CPAN Attribute module but here you get extra information for the subroutine, which use the attribute. E.g. the resolved methodname.

This helper class is used from Fukurama::Class::Attribute::OOStandard to enable the OO-method-signatures.

EXPORT

MODIFY_CODE_ATTRIBUTES

would be decorated if it exist or created if it isn't in the current class.

METHODS

register_attributes( attribute_handler_class:STRING ) return:BOOLEAN

Register a handler class which defines attributes. See "How to define an attribute-handler-class" below

export( export_to_class:STRING ) return:BOOLEAN

This will export or decorate the MODIFY_CODE_ATTRIBUTES to the export_to_class class. Be sure that you call this method in a BEGIN block. Perl check them all at compiletime and croak, if some is not defined.

get_registered_subs( ) return:HASHREF

Get the method-definitions from all methods in your code, which use attributes over this attribute handler. This is to check th code structure (or to create some documentation...)

register_helper_method( methodname:STRING ) return:VOID

All registered methodnames would be omitted as attributes, when a attribute-handler-class is parsed. But if they are missed in a attribute-handler-class, the registration would fail.

run_check( ) return:VOID

Resolve all method names, which are unresolved at compiletime, and calls the atribute-definition-methods in the handler-class. This is a helper method for static perl (see Fukurama::Class > BUGS)

How to define an attribute-handler-class

All methods of an attribute-handler-class have to be attribute-definitions, except these, which are registered via register helper methods. This methods have to start with an uppercase letter (it is a perl specification). They will get a hash reference as single parameter. In this hash you will find information of the method which use your attribute. They are:

class:STRING

The name of the class, which contain the subroutine which use the attribute (*puh*). Can be empty in some cases. Look at resolved.

sub_name:STRING

The resolved name of the subroutine, which use the attribute. Perls attributes doesn't resolve the name by itself, so you will normally only get the sub-reference and not the name. It can be empty in some cases. Look at resolved.

data:STRING

The defined attribute-data. if you say 'sub new : MyAtt(this is a $test)' you will get the string 'this is a $test'.

sub:CODEREF

The code-refrence of the subroutine, which use the attribute.

resolved:BOOLEAN

A flag for the status of method name resolving for this method. In some cases, if you force a call, this flag will be FALSE and the sub_name will be empty.

attribute:STRING

The name of the attribute. This is the same like the name of your attribute-method.

handler:HASHREF

A reference to your attribute class and to the actual attribute method.

executed: BOOLEAN

An internal flag to avoid double callings of your attribute-methods.

AUTHOR, BUGS, SUPPORT, ACKNOWLEDGEMENTS, COPYRIGHT & LICENSE

see perldoc of Fukurama::Class