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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.