Nifty! But why all of that business with grep to get the odd and even elements of the @replacement_pairs? How about something like this instead:

sub rebind_closure { my $sub = shift; ref($sub) eq 'CODE' or die "First parameter must be a code ref (cl +osure)"; @_ % 2 or die "Rules must be an even numbered list: @_"; my ($names,@values) = svref_2object( $sub )->PADLIST->ARRAY; my @names = @{$names->object_2svref}; while (($rule,$replacement) = (shift, shift)) { for ( 1 .. $#names ) { next unless rule_matches( $rule, $names[$_] ); my $sigil = substr $names[$_], 0, 1; if ( $sigil eq '$' ) { ${($values[0]->ARRAY)[$_]->object_2svref} = $replaceme +nt; } elsif ( $sigil eq '@' ) { @{($values[0]->ARRAY)[$_]->object_2svref} = @$replacem +ent; } elsif ( $sigil eq '%' ) { %{($values[0]->ARRAY)[$_]->object_2svref} = %$replacem +ent; } elsif ( $sigil eq '*' or $sigil eq '&' ) { warn "$names[$_] cannot be rebound because it is not a + scalar, array or hash"; } else { warn "Unknown sigil $sigil"; } } } }

As usual, caveat lector, I didn't test this code


In reply to Re: Rebinding closures by duff
in thread Rebinding closures 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.