in reply to Rebinding closures
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Rebinding closures
by diotalevi (Canon) on Dec 17, 2003 at 01:39 UTC | |
by duff (Parson) on Dec 17, 2003 at 01:50 UTC | |
by diotalevi (Canon) on Dec 17, 2003 at 03:28 UTC |