As far as I know, there are basically three ways to define and use subroutine attributes in the Perl environment:

The built-in attributes are integrated into the compilation process and can affect what counts as legal syntax (e.g. lvalue). In addition to the two built-in attributes listed above, there are two version specific subroutine attributes that you might see in older code:

As there are very few built-in attributes, Perl and its extensions provide two mechanisms for custom attribute definitions. Common reasons to use custom attributes are

  1. generation of a wrapper function that inserts code before and after the subroutine call. The code assigned to the subroutine name is replaced by the code making before+call+after code. For example, you might want to log the input parameters and return values of a call.
  2. generation supplemental functions and data based on the actual attributes.
  3. compilation messages and metadata statistics for use by frameworks (e.g. Catalyst and MooseClass::Std).

Custom attributes defined using FETCH_CODE_ATTRIBUTES and MODIFY_CODE_ATTRIBUTES can be used safely under mod_perl but only handle use cases 2 and 3 above. They can't be used to generate wrappers that replace subroutine definitions (use case 1) because MODIFY_CODE_ATTRIBUTES is called before the function body has been defined and inserted into the symbol table.

Attribute::Handlers can be used for all three purposes, but it relies on the CHECK and INIT phases for loading up a module and these don't get triggered under mod_perl or in other environments that rely on eval to load code. The issues related to the two different methods for defining custom attributes has been discussed at length in the following threads:

For examples using customized attributes, see the following links:

Best, beth


In reply to Re: Where subroutine attributes will be used? by ELISHEVA
in thread Where subroutine attributes will be used? by anbutechie

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.