in reply to In Search of a Smarter Sub
The idea is that you have a set of structures, just like your nested if-statements. You want to have a set of behaviors in that nested-if and you don't want to have to hard-code it all over the place.
sub nested_ifs { my ($i_max, $j_max, @behaviors) = @_; foreach my $i ( 1 .. $i_max ) { foreach my $j ( 1 .. $j_max ) { foreach my $behavior ( @behaviors ) { $behavior->( $i, $j ); } # Or, more succinctly: # $_->( $i, $j ) for @behaviors; } } } sub func1 { ... } sub func2 { ... } nested_ifs( 10, 20, \&func1, \&func2, sub { my ($i,$j) = @_; print "I: + $i\n\tJ: $j\n"; } );
|
|---|