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

I'm trying to setup attributes for a module that emulates the behavior of threads::shared. So instead of
my $foo : shared = 'foo';
I'd have
my $foo : Sociable = 'foo';
Note I'm trying to avoid the fully qualified syntax, i.e.,
my Thread::Sociable $foo : Sociable = 'foo';
Which presumably means I need to install my attribute handlers into the UNIVERSAL space. Which does seem to work OK.

I've tried using Attributes::Handler, but with marginal success...things behave very badly when run under the debugger.

My concern is that my solution is tromping all over other attributes apps. At present, I'm using the following bit of code to init the handlers:

our $old_scalar_attr = \&UNIVERSAL::MODIFY_SCALAR_ATTRIBUTES; our $old_array_attr = \&UNIVERSAL::MODIFY_ARRAY_ATTRIBUTES; our $old_hash_attr = \&UNIVERSAL::MODIFY_HASH_ATTRIBUTES; *UNIVERSAL::MODIFY_SCALAR_ATTRIBUTES = \&SociableScalar; *UNIVERSAL::MODIFY_ARRAY_ATTRIBUTES = \&SociableArray; *UNIVERSAL::MODIFY_HASH_ATTRIBUTES = \&SociableHash;
and then calling the &$old_XXX_attr() with appropriate args after my SociableXXX() is done pulling out the attributes its interested in.

Am I way off base ? Is there a better way ? Any pointers (esp. to any code examples that do the same) much appreciated!

Replies are listed 'Best First'.
Re: Attribute confusion
by friedo (Prior) on Oct 19, 2006 at 20:56 UTC
    Are you sure you need to install your attribute handlers in UNIVERSAL? Why not just export them?
Re: Attribute confusion
by chromatic (Archbishop) on Oct 19, 2006 at 21:10 UTC

    friedo is right; export your attribute sub into the using namespace. You might also prefer Attribute::Handlers, as its documentation is readable. I've never understood the old interface.

      OK, export works, but...what if, e.g., the app wants to use multiple attributes handler modules ? Or some other attribute module needs to use my module ? When I created another stub attributes handler module, and use'd both modules, and applied both sets of attributes, only the last handler got invoked, presumably since its version of MODIFY_XXX_ATTRIBUTES replaced by the 1st module's version ?

      Wrt Attributes::Handler, while I agree that the attributes.pm POD is only useful if you already know how to use attributes, my reasons for not using Attribute::Handler (as noted in my OP) arose from the following error occuring when trying to use the attributes under "perl -d" (on AS 5.8.6 WinXP) :

      test_count set to 2 Test 1 Can't use an undefined value as a symbol reference at C:/Perl/lib/Attr +ibute/Han lers.pm line 16. at C:/Perl/lib/Attribute/Handlers.pm line 16 Attribute::Handlers::findsym('main', 'SCALAR(0x1b6e5cc)') call +ed at C:/ erl/lib/Attribute/Handlers.pm line 180 Attribute::Handlers::_apply_handler_AH_('ARRAY(0x1c3475c)', 'C +HECK') ca led at C:/Perl/lib/Attribute/Handlers.pm line 146 Attribute::Handlers::__ANON__[C:/Perl/lib/Attribute/Handlers.p +m:165]('m in', 'SCALAR(0x1b6e5cc)', 'Sociable') called at C:/Perl/lib/attributes +.pm line 6 attributes::import('attributes', 'main', 'SCALAR(0x1b6e5cc)', +'Sociable ) called at t\02shared_attr.t line 52 Debugged program terminated. Use q to quit or R to restart, use O inhibit_exit to avoid stopping after program termination, h q, h R or h O to get additional info. DB<1>

        Your questions are why I use Attribute::Handlers. If this were my project, I'd file a bug and hope someone came up with an easy-to-port fix.