Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Perl Monk, Perl Meditation.
 
PerlMonks

Installing chained methods

by diotalevi (Canon)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Apr 21, 2003 at 02:15 UTC ( #251908=perlquestion: print w/ replies, xml ) Need Help??
diotalevi has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to install one or a few methods into a given slot. If no method already exists in a given slot then my copy should be directly copied. If I need a second or more method to then I need to have a stub method call each of the real methods. While the actual code for this is pretty simple I think this far too messy for something in a database application and I don't want to force volunteer programmers to cope with this.

This is intended to be used in an application using the Alzabo OR mapping system and in this case I need to call both an auditing and versioning method when the pre_insert event occurs. For one method call I'd just copy *pkg::pre_insert = *auditing_pre_insert and leave it at that but since it isn't that trivial I don't feel comfortable doing this right in the application.

I'm hoping someone here knows of a nice module for this so I can use that instead. Suggestions?

if (*{"GreenPartyDB::Database::${table}::${hook}"}{CODE}) { my $old = *{"GreenPartyDB::Database::${table}::${hook}"}{CODE}; my $new = *{__PACKAGE__."::${prefix}${hook}"}{CODE} *{"GreenPartyDB::Database::${table}::${hook}"} = sub { $_[0]->$old( @_[ 1 .. $#_ ] ); $_[0]->$new( @_[ 1 .. $#_ ] ); }; } else { *{"GreenPartyDB::Database::${table}::${hook}"} = *{__PACKAGE__."::${prefix}${hook}"}{CODE}; }

Comment on Installing chained methods
Select or Download Code
Re: Installing chained methods (Hook::*?)
by Aristotle (Chancellor) on Apr 21, 2003 at 02:33 UTC

      Nothing in the Hook:: class namespace does the right thing here (including Class::Hook). Thank you though. All the Hook:: modules assume that the method already exists and I'm looking at it from the other direction - initially the method doesn't exist and the code just installs it somewhat like Exporter.

      Oh well - absent any good suggestions (after waiting a while) I'll just write the darn thing and upload it. Thank you for suggesting Hook:: though - that search keyword didn't occur to me.

        Aha, I see. Maybe like so?
        use Hook::WrapSubs; do { no strict 'refs'; *{"GreenPartyDB::Database::${table}::${hook}"} = sub {}; } unless UNIVERSAL::can("GreenPartyDB::Database::${table}", $hook) wrap_subs( "GreenPartyDB::Database::${table}::${hook}", UNIVERSAL::can(__PACKAGE__."::${prefix}", $hook), );
        Note that using UNIVERSAL::can() will respect inherited methods, which is not unlikely to be better than what you started out with (or may be not, of course - your call).

        Makeshifts last the longest.

Login:
Password
remember me
What's my password?
Create A New User

Node Status?
node history
Node Type: perlquestion [id://251908]
Approved by Aristotle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (16)
jdporter
holli
Gavin
atcroft
kennethk
thezip
Eyck
renodino
metaperl
crashtest
roho
ssandv
je44ery
JStrom
MikeDexter
pramod8785
As of 2010-02-09 23:01 GMT
Sections?
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information?
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes?
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers?
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth?

What level of existential comfort do you require?

Palace
Executive suite at the best hotel
Regular hotel in a decent part of town
Motel
Boarding house
Sleeping Bag on Couch in Basement
Any port in a storm
Camping under the freeway overpass
Jail
Other

Results (283 votes), past polls