Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I want to 'push' a subroutine into a class. By this I mean if a class has a 'foo' method, I want to grab a reference to the old subroutine, and replace it with a subroutine that Does Other Stuff and then calls the old one.

This is made slightly trickier in that I want to do it in a way that I can use it across multiple modules, as long as the routine I want to add is the same.

So, for instance
package Parenthise; sub parenthise { my($class, $field_name) = @_; my $fullname = join ('::', $class, $field_name); my $coderef; my $set_coderef = "\$coderef = $fullname;"; eval $set_coderef; my $replace_sub = sub { my ($self, $value) = @_; if(@_ > 1){ return $coderef->($self); }else{ return $coderef->($self, "($value)"); } }; # here's where the trouble starts my $switcheroo = sprintf(<<"HERE" , $class, $field_name, $ +field_name); package %s; undef &{ *%s{CODE} }; *%s = \$coderef; HERE eval $switcheroo; } 1;
package SomePackage; use Class::MethodMaker get_set => [qw/value/], new_with_init => 'new'; use Parenthise; Parenthise::parenthise(__PACKAGE__, 'value'); 1;
use Somepackage; my $thing = Somepackage->new(); $thing->value('bar'); $thing->value eq '(bar)';

Note that this isn't exactly what my code is, my actual code is a bit more complicated by the fact that there are multiple fields and the function is actually figuring out which fields to change automatically.

I haven't tested the code posted in this comment, as all the interesting bits are copy and paste from my code.

I am trying to get this to work under strict, which is why the undef in the switcheroo eval.

Thanks tons for any and all help in advance, I've been banging my head on this for quite a while trying various things.

20040720 Edit by ysth: break up (accidental?) extremely long code line


In reply to Advanced subroutine and symbol table manipultation by Tuppence

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-29 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found