I didn't realize till after compiling an example that this was a rather old post, but I'll post it anyway. Here is an example of using attributes to added debug information to a sub (printing out its @_ before and after execution) as well as a way to wrap some errors/warnings around methods. Both of these use Hook::LexWrap to warp extra code around the function being attributed...attributized, attributated?</code>

use strict; use warnings; { package tools; use Data::Dumper; use Hook::LexWrap; use Attribute::Handlers; sub UNIVERSAL::Debug :ATTR(CODE) { wrap $_[1], pre => sub { pop; print Dumper({'Pre' => \@_ + })}, post => sub { pop; print Dumper({'Post' => \@_ + })}, } sub UNIVERSAL::Method :ATTR(CODE) { my ($package, $symbol, $referent, $attr, $data, $phase) = +@_; wrap $_[1], pre => sub { my $self = shift; die "Must be used as a method" +unless ref($self) eq $package; }; } } { package Test; sub method_test :Method { print "This should be called on a reference\n"; } } package main; sub foo : Debug { print "This is the end!\n"; return 123;}; foo(1,2,3); my $x = bless {}, 'Test'; $x->method_test(); Test::method_test('hello'); __DATA__ $VAR1 = { 'Pre' => [ 1, 2, 3 ] }; This is the end! $VAR1 = { 'Post' => [ 1, 2, 3 ] }; This should be called on a reference Must be used as a method at attributes.pl line 16.


___________
Eric Hodges

In reply to Re: How Are Attributes Useful? by eric256
in thread How Are Attributes Useful? by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.