This is something like a request for comments.

Playing around with attributes (as per Attribute::Handler), I've done
several more attribute handlers and have also bundled a few into one
module but am not quite sure what to call it. First, here are examples
of those handlers:

1) Attribute::Tools

        sub fib :Memoize       { ... }    # like Attribute::Memoize
        sub foo :Abstract      { ... }    # like Attribute::Abstract
        sub color :aka(colour) { ... }    # alternative names

        sub mywarn : SigHandler(__WARN__) { ... }
        # instead of $SIG{__WARN__} = \&mywarn;
        # keeps you free from implementation details

        sub myadd : Overload(+) { ... }
        # instead of use overload '+' => \&myadd;

The above five attributes are in one module called Attribute::Tools, but
maybe someone has a better name for them. It seems like a waste to have
a separate CPAN module for each of those.

Now some more attributes:

2) Attribute::Export

        use Attribute::Export;
        sub hello : Export { "hello there" }
        sub askme : ExportOk { "export is ok" }
        # shields you from the Exporter arrays

3) Attribute::INC

        use Attribute::INC;
        sub traceinc : INC {
                my ($self, $file) = @_;
                print "looking for $file?\n";
                return;
        }
        # installs a coderef-in-@INC

4) Attribute::Documentation

        use Attribute::Documentation 'document_module';
        document_module
            Description => 'Just a sample module',
            Author      => 'Marcel Grunauer <[EMAIL PROTECTED]>';
        sub new : Description(The constructor) { bless {}, shift }
        sub old : Deprecated :Public { print "something\n" }
        # remembers this documentation in a hash structure so you
        # can query it later or generate POD from it

(on a related note, it'd be nice to use Class::Contract and/or
Class::MethodMaker to autogenerate documentation for the classes
generated by them)

5) Future idea: XML template match attributes

An XSL-like declarative XML transformation mechanism using a
multimethod-like mechanism implemented via attributes:

     sub apply :XPathMatch(//xyz[@name="abc"]/def) { ... }

which is expected to return the transformed text, much like an XSL
template does, except it has the power of Perl behind it

6) Future idea: PreAttrHook, PostAttrHook, AUTOATTR

Attribute::Handler could be extended so it recognizes those three
new attributes and calls the PreAttrHook handler before the first
attribute handler on a symbol, and the PostAttrHook handler on the
last one. This way you could override the behavior of any other
handlers, or just keep track of what attributes there are (so you
can report on them later).

If any attribute is used for which there isn't a handler, but there
is an AUTOATTR handler, that one is called instead.

7) Future idea: DefaultAttr

Maybe using a source filter, it might be possible to add an attribute
(let's call it ':DefaultAttr') to each symbol that can possibly have
an attribute, so you can use attributes to do things to each and every
variable and subroutine. This would be really useful for bringing in
aspect-oriented programming. For example, if you wanted to trace all
calls to sub in the Foo package, you might say:

        sub DefaultAttr : ATTR(CODE) {
                my ($pkg, $symbol) = @_;
                print STDERR "entering $symbol\n" if $pkg eq 'Foo'
        }


Any comments, ideas, or contact details of the nearest psychiatric
clinic would be appreciated.

--
$ perl -we time
Useless use of time in void context at -e line 1.

Reply via email to