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

  • SEARCH
  • Home
  • 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 6910673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:50:41+00:00 2026-05-27T08:50:41+00:00

I don’t grok Perl subroutine attributes at all. I have never seen them in

  • 0

I don’t grok Perl subroutine attributes at all.

I have never seen them in actual code and perldoc perlsub and the perldoc attributes fail to answer my questions:

  • What are attributes useful for?
  • What do they bring to the table that is not already present in Perl best practices?
  • Are there any CPAN modules (well-known or otherwise) that make use of attributes?

It would be great if someone could put together a detailed example of attributes being used the way they should be.


For those who are as clueless as me, attributes are the parameters after the colon in the attributes SYNOPSIS examples below:

sub foo : method ;
my ($x,@y,%z) : Bent = 1;
my $s = sub : method { ... };

use attributes ();  # optional, to get subroutine declarations
my @attrlist = attributes::get(\&foo);

use attributes 'get'; # import the attributes::get subroutine
my @attrlist = get \&foo;
  • 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-27T08:50:42+00:00Added an answer on May 27, 2026 at 8:50 am

    Attributes allow you annotate variables to perform auto-magic behind the scenes. A similar concept is java annotations. Here is a small example that might help. It uses Attribute::Handlers to create the loud attributes.

    use Attribute::Handlers;
    
    sub UNIVERSAL::loud : ATTR(CODE) {
        my ( $pkg, $sym, $code ) = @_;
        no warnings 'redefine';
        *{$sym} = sub {
            return uc $code->(@_);
        };
    }
    
    sub foo : loud {
        return "this is $_[0]";
    }
    
    say foo("a spoon");
    say foo("a fork");
    

    Whenever a sub is declared with the loud attribute the UNIVERSAL::loud callback triggers exposing meta-information on the sub. I redefined the function to actually call an anonymous sub, which in turn calls the original sub and passes it to uc

    This outputs:

    THIS IS A SPOON
    THIS IS A FORK
    

    Now let’s looks a the variable example from the SYNOPSIS:

    my ($x,@y,%z) : Bent = 1;
    

    Breaking this down into small perl statement without taking into account attributes we have

    my $x : Bent
    $x = 1;
    
    my @y : Bent
    @y = 1;
    
    my %Z : Bent
    %z = 1;
    

    We can now see that each variable has been attributed the Bent annotation in a concise way, while also assigning all variables the value 1. Here is a perhaps more interesting example:

    use Attribute::Handlers;
    use Tie::Toggle;
    
    sub UNIVERSAL::Toggle : ATTR(SCALAR) {
        my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
        my @data = ref $data eq 'ARRAY' ? @$data : $data;
        tie $$referent, 'Tie::Toggle', @data;
    }
    
    my $x : Toggle;
    
    say "x is ", $x;
    say "x is ", $x;
    say "x is ", $x;
    

    Which outputs:

    x is 
    x is 1
    x is 
    

    You can use this to do logging, create test annotations, add type details to variables, syntactic sugar, do moose-ish role composition and many other cool things.

    Also see this question: How do Perl method attributes work?.

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

Sidebar

Related Questions

don't know better title for this, but here's my code. I have class user
Don't they both have to convert to machine code at some point to execute
Don't yell at me! i have seen many threads claiming that HTML cannot be
Don't be scared of the extensive code. The problem is general. I just provided
Don't be frightened, its a very basic code. Just wanted to check with you
Don't overthink this - there's a very commonly used term and I ... have
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't think my virtualhost is working correctly. This is what I have inside of
Don't ask why but I have the requirement to draw a border around certain
DON'T ASK WHY but... I have a regex that needs to be case insensitive

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.