Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 243865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:57:25+00:00 2026-05-11T20:57:25+00:00

A little known built-in Perl feature is attributes. However, the official documentation is doing

  • 0

A little known built-in Perl feature is attributes. However, the official documentation is doing a rather bad job introducing newbies to the concept. At the same time, frameworks like Catalyst use attributes extensively which seems to make many things easier there. Since using something without knowing the implications sucks a bit, I’d like to know the details. Syntax-wise they look like Python’s decorators, but the documentation implies something simpler.

Could you explain (with real-world examples if possible) what attributes are good for and what happens behind the doors?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:57:25+00:00Added an answer on May 11, 2026 at 8:57 pm

    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:

    sub some_method :Foo { }
    

    Perl will while compiling your program (this is important) look for the magic sub MODIFY_CODE_ATTRIBUTES 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.

    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.

    There is another handler called FETCH_CODE_ATTRIBUTES that will be called whenever someone says

    use attributes;
    my @attrs = attributes::get(\&some_method);
    

    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).

    Here is an example to enable simple "tagging" of methods with arbitrary attributes, which you can query later:

    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;
    

    Now, in MyClass and all its subclasses, you can use arbitrary attributes, and query them using attributes::get():

    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->can('hello'));
    
    1;
    __END__
    hello() in SomeClass has attributes: Foo, Bar
    

    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 Mike Friedman’s article), or for your own devious purposes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know a little about SNMP, but not enough. I need to develop an
I know very little about JavaScript but despite this I'm trying to cobble something
I am a little curious to know about how OpenID authentication works. Is there
I'm a PHP developer who knows a little bit of Ruby. I want to
First question on here so please be nice :) I know very little about
I just started thinking about creating/customizing a web crawler today, and know very little
I know it makes little difference to a project but, assuming you use #defined
I know this question might sound a little cheesy but this is the first
I faced a little trouble - I do not know if I can define
Now that LINQ to SQL is a little more mature, I'd like to know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.