Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are right, the documentation is not very clear in this area, especially since attributes are not so complicated. If you define a subroutine attribute, like this:</p> <pre><code>sub some_method :Foo { } </code></pre> <p>Perl will <em>while compiling your program</em> (this is important) look for the magic sub <code>MODIFY_CODE_ATTRIBUTES</code> in the current package or any of its parent classes. This will be called with the name of the current package, a reference to your subroutine, and a list of the attributes defined for this subroutine. If this handler does not exist, compilation will fail.</p> <p>What you do in this handler is entirely up to you. Yes, that's right. No hidden magic whatsoever. If you want to signal an error, returning the name of the offending attributes will cause the compilation to fail with an "invalid attribute" message.</p> <p>There is another handler called <code>FETCH_CODE_ATTRIBUTES</code> that will be called whenever someone says</p> <pre><code>use attributes; my @attrs = attributes::get(\&amp;some_method); </code></pre> <p>This handler gets passed the package name and subroutine reference, and is supposed to return a list of the subroutine's attributes (though what you really do is again up to you).</p> <p>Here is an example to enable simple "tagging" of methods with arbitrary attributes, which you can query later:</p> <pre><code>package MyClass; use Scalar::Util qw( refaddr ); my %attrs; # package variable to store attribute lists by coderef address sub MODIFY_CODE_ATTRIBUTES { my ($package, $subref, @attrs) = @_; $attrs{ refaddr $subref } = \@attrs; return; } sub FETCH_CODE_ATTRIBUTES { my ($package, $subref) = @_; my $attrs = $attrs{ refaddr $subref }; return @$attrs; } 1; </code></pre> <p>Now, in MyClass and all its subclasses, you can use arbitrary attributes, and query them using <code>attributes::get()</code>:</p> <pre><code>package SomeClass; use base 'MyClass'; use attributes; # set attributes sub hello :Foo :Bar { } # query attributes print "hello() in SomeClass has attributes: ", join ', ', attributes::get(SomeClass-&gt;can('hello')); 1; __END__ hello() in SomeClass has attributes: Foo, Bar </code></pre> <p>In summary, attributes don't do very much which on the other hand makes them very flexible: You can use them as real "attributes" (as shown in this example), implement something like decorators (see <a href="https://stackoverflow.com/questions/987059/how-do-perl-method-attributes-work/987216#987216">Sinan's answer</a>), or for your own devious purposes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload