Eh, I've a hard time believe there exists a situation where it [statically splicing the code, as in cog's solution] wouldn't work, but where it's still possible to dynamically splicing in code blocks while code is running.
Could you give an example of such code?
BTW, what you are asking for is a special case of "loop enlightment", and cog's solution is the standard way of solving this. It belongs in Programming 102. | [reply] |
for ( @some_array ) {
if ( $_ eq 'foo' ) {
print "skipping foo\n";
next;
}
if ( $_ eq 'bar' ) {
handle_bar($_);
next;
}
handle_rest($_);
}
To re-write this we need to first change
for ( @some_array ) { ... }
# to
my $index = -1;
while ( ++$index <= $#some_array ) { ... }
So that when we break out of the first loop to enter the second, we can remember where we left off. Unfortunately, the order that we will encounter 'foo' and 'bar' is unknown so we also have to create a flag variable and end up with 4 while loops instead of the original 1:
This was a translation of a very simple example. I do not if there is any case where it would be impossible to do, but it certainly isn't easy. Again, the point of the medidation is to just take the functionality as a given for a second and think about what you would do with it.
| [reply] [d/l] [select] |
for ( @some_array ) {
if ( ?^foo$? ) {
print "skipping foo\n";
next;
}
if ( ?^bar$? ) {
handle_bar($_);
next;
}
handle_rest($_);
}
Caution: Contents may have been coded under pressure.
| [reply] [d/l] |
my %things = (foo => sub {print "skipping foo\n"; next},
bar => sub {handle_bar($_); next},
);
for (@some_array) {
(delete $things{$_})->() if exists $things{$_};
handle_rest($_);
}
| [reply] [d/l] |