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??

One evening's sleep and I'll agree that I'm likely to break stuff this way especially since all the module's subroutines would have to be recompiled to use the data in the object instead of from the lexicals. Yick.

I was musing this morning of breakfast that I have a bad habit of jumping off of more bridges than I have to - use of one work around doesn't justify the use of yet another. Somewhere along the way after I've convinced myself that whatever thing I had to do to make something else work (that was necessary) I somehow get the idea that "just a bit more" won't hurt. And eventually I end up with icky things like this:

BEGIN{die "No. You should not be using this code.";} package YAPE::Regex::EvilPatch; use YAPE::Regex (); use B qw(svref_2object); use Devel::Pointer qw(unsmash_sv unsmash_hv); use B::Deparse (); sub patch { return if $_[0]->{'pat'} or %YAPE::Regex::pat; my $var; # Copy the lexicals into globals. Also throw away the # compiled versions of all of the regular expressions. $var = fetch_pad_var( \ &YAPE::Regex::_ok_class, '$valid_POSIX' ); $YAPE::Regex::valid_POSIX = ''.unsmash_sv( $$var ); $var = fetch_pad_var( \ &YAPE::Regex::_ok_class, '$ok_cc_REx' ); $YAPE::Regex::ok_cc_REx = ''.unsmash_sv( $$var ); $var = fetch_pad_var( \&YAPE::Regex::next, '%pat' ); %YAPE::Regex::pat = map "$_", %{unsmash_hv( $$var )}; my $deparse = B::Deparse->new; # Patch the ->new method so it copies %YAPE::Regex::pat my $new_m = $deparse->coderef2text( \ &YAPE::Regex::new ); $new_m =~ s/(?<=bless\({)/'PAT', {%pat},/ or die "Couldn't add PAT to new"; $new_m = "sub $new_m;"; *YAPE::Regex::new = eval $new_m; # Alter ->next so it fetches from $self->{'PAT'} my $next = $deparse->coderef2text(\&YAPE::Regex::next); $next =~ s/(?<=\$)pat(?=\{)/self->{'PAT'}/g; *YAPE::Regex::next = eval "sub $next;"; # Same thin for ->_get_quant my $quant = $deparse->coderef2text(\ &YAPE::Regex::_get_quant); $quant =~ s/(?<=\$)pat(?=\{)/self->{'PAT'}/g; *YAPE::Regex::_get_quant = eval "sub $quant"; # _ok_class uses all three lexicals. Same deal. } sub fetch_pad_var { my $cv = shift; my $var = shift; my $ocv = svref_2object( $cv ); my ($names, $values) = map [ $_->ARRAY ], $ocv->PADLIST->ARRAY; my $h; for my $i (0 .. $#$names) { if ( $names->[$i]->can('PV') and $names->[$i]->PV eq $var ) { return $values->[$i]; } } return (); }

In reply to Re: Stealing lexicals - best practice suggestions by diotalevi
in thread Stealing lexicals - best practice suggestions by diotalevi

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 chanting in the Monastery: (6)
As of 2024-03-28 16:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found