sub process { my ($data, $filter) = @_; # lengthy calculations with $data here ... # in that length calculation you have to decide # if you continue with your calculation: for (@$data){ if ($filter) { if ($filter->($_)){ push @$data, other_lengthy_calculation($_); } } else { push @$data, other_lengthy_calculation($_); } } # second option: sub process { my ($data, $filter) = @_; # provide a default value for $filter $filter = sub {1;} unless $filter; # lengthy calculations with $data here ... # in that length calculation you have to decide # if you continue with your calculation: for (@$data){ if ($filter->($_)) { push @$data, other_lengthy_calculation($_); } }