in reply to Re^3: what is sub {1;}
in thread what is sub {1;}
Sure, it involves more lines (12 now instead of ~8 lines), but why you would deliberately want to throw a costly sub call in a loop? If @$data is huge and $filter is usually empty, using '$filter = sub {1;}' could quickly become a bottleneck.# third option: sub process { my ($data, $filter) = @_; # lengthy calculations with $data here ... # in that length calculation you have to decide # if you continue with your calculation: if ($filter) { for (@$data){ if ($filter->($_)) { push @$data, other_lengthy_calculation($_); } } } else { for (@$data){ push @$data, other_lengthy_calculation($_); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: what is sub {1;}
by moritz (Cardinal) on May 27, 2008 at 14:14 UTC |