Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Where subroutine attributes will be used?

by anbutechie (Sexton)
on Mar 09, 2009 at 04:59 UTC ( [id://749204]=perlquestion: print w/replies, xml ) Need Help??

anbutechie has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
What is subtroutine attribute and where it can be used.
If possible give me an example
regards
Anbarasu
  • Comment on Where subroutine attributes will be used?

Replies are listed 'Best First'.
Re: Where subroutine attributes will be used?
by ELISHEVA (Prior) on Mar 09, 2009 at 15:53 UTC
    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:

    • locked which was used in the threaded version of Perl 5.005 and deprecated thereafter.
    • assertion which was added in 5.9.0 and removed in 5.10.0 because it made code disappear when maybe it shouldn't - see the assertion pragma for a discussion and further links.

    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

      ... compilation messages and metadata statistics for use by frameworks (e.g. Catalyst and Moose).

      Actually, Moose does not and never will use attributes. As perrin says below, they are best avoided at all costs. The API to use them is horrid and the limitations of that API are (IMO at least) totally unacceptable. Even Catalyst will eventually do away with attributes in favor of using something similar to how Moose works or something more radical like Devel::Declare.

      -stvn
Re: Where subroutine attributes will be used?
by perrin (Chancellor) on Mar 09, 2009 at 17:07 UTC
    My advice is to avoid them in almost all circumstances. They make your code harder to read and since they are just strings and not parsed by perl they get really ugly when people try to jam anything that looks like code into them.
      My advice is to avoid them in almost all circumstances.

      I was reading Ovid's article on Test::Class just now, and noticed that Test::Class does use subroutine attributes rather extensively.

      I'd appreciate if you could comment on that. Is Test::Class the exception in "almost"? Or do you and the author of Test::Class simply disagree on the usefulness of attributes?


      All dogma is stupid.
        I use Test::Class. The sub attributes it uses are relatively simple (nothing too perl), which is good because it makes them harder to screw up. However, I don't find them an improvement over a simple config data structure.
Re: Where subroutine attributes will be used?
by Anonymous Monk on Mar 09, 2009 at 05:19 UTC
Re: Where subroutine attributes will be used?
by f00li5h (Chaplain) on Mar 09, 2009 at 05:45 UTC

    Catalyst uses them in Controllers to tell the dispatcher where to send requests

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;

Re: Where subroutine attributes will be used?
by Anonymous Monk on Mar 09, 2009 at 05:13 UTC
Re: Where subroutine attributes will be used?
by sundialsvc4 (Abbot) on Mar 09, 2009 at 13:45 UTC

    (Response withdrawn.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://749204]
Approved by ikegami
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found