Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Attribute Handlers don't run when called at run time.

by bennymack (Pilgrim)
on Sep 04, 2008 at 15:56 UTC ( [id://709047]=note: print w/replies, xml ) Need Help??


in reply to Re: Attribute Handlers don't run when called at run time.
in thread Attribute Handlers don't run when called at run time.

Tilly, that's actually very similar to what I've been doing in the meantime to work around this. I just wanted to look for a solution that would let me use the cleaner looking attributes

Here is an actual example of my work around:

sub _is_inactive { my( $self ) = @ARG; my( $realword ) = $self->realword; return $realword =~ $self->CHEVRON_QR; } install_sub( { code => __PACKAGE__->hash_instance_cache( 'sub' => '_is_inactive' +), as => 'is_inactive' } );

As you may have guessed, I'm using all this to automatically cache the return value of the sub in the hash based object instance. A different issue altogether I'm sure ;) I just found myself working with references to hash values which can be repetitive and error prone so I abstracted it into an attribute but the that is also abstracted into something that can be used separately from attribute handlers.

Replies are listed 'Best First'.
Re^3: Attribute Handlers don't run when called at run time.
by tilly (Archbishop) on Sep 04, 2008 at 17:15 UTC
    Three responses.

    My first one is that this smells like micro-optimization. In general micro-optimization is a bad idea. Keep your code clean and wait until you have a documented issue before you try to optimize it.

    The second is that if you're only doing one thing with the sub, you can massively simplify your API to the following:

    install_sub("is_inactive", sub { my( $self ) = @ARG; my( $realword ) = $self->realword; return $realword =~ $self->CHEVRON_QR; });
    The third piece of advice is that when I have to work with hashes I sometimes find it very useful to use Hash::Util's lock_keys method to avoid trying to access a key that isn't there. Think of it as strict.pm for hashes. Sure, it is slower. But it is easy to make turning it on conditional. (Though unless I really need the performance, I usually just leave it on.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-28 07:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found