Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Sub attributes

by lodin (Hermit)
on Jun 10, 2009 at 11:23 UTC ( #770294=note: print w/replies, xml ) Need Help??

in reply to Sub attributes

I'm not sure I understand the question. Maybe this code example is what you're looking for?

use strict; use List::Extract 'extract'; sub MODIFY_CODE_ATTRIBUTES { my $class = shift; my ($cref, @attrs) = @_; my ($bent) = extract { /^Bent\b/ } @attrs; if (defined $bent) { print "Recognized attribute: $bent\n"; } return @attrs; # Unrecognized. } sub foo :Bent; sub bar :Bent(named) { 1 } my $code = sub :Bent(anonymous) { 1 } __END__ Recognized attribute: Bent Recognized attribute: Bent(named) Recognized attribute: Bent(anonymous)
This is documented in attributes under "Package-specific Attribute Handling".

What to do once a subroutine attribute is recognized is a completely different story. :-)


Replies are listed 'Best First'.
Re^2: Sub attributes
by John M. Dlugosz (Monsignor) on Jun 10, 2009 at 16:14 UTC
    So only built-in attributes are recognized globally? That would contradict the examples. He's not setting up any package-attribute handler; just using Bent. And cautioning against unrecognized in nearly the same breath.

      So only built-in attributes are recognized globally?

      Correct. And that is how it should be. You don't want global attributes any more than you want global subroutines. I'm not saying that I like the current implementation, but at least its good that the attributes are somewhat scoped.

      Since the handlers (MODIFY_CODE_ATTRIBUTES etc) are inherited one can add them to UNIVERSAL and achieve global attributes. Attribute::Handlers for instance uses this trick. This however makes it very dangerous to use attributes in modules, since attributes defined in different modules may clash. See also my previous meditation on the problems with attributes (it also links to other nodes about attributes).


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://770294]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2022-01-25 01:47 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (65 votes). Check out past polls.