in reply to Re^3: what is sub {1;}
in thread what is sub {1;}

# 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($_); } }
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.

I agree with the OP, use of a sub {1;} to avoid 'undefined' doesn't make much sense (to me anyway), unless the application interface guarantees that parameter to be a sub reference all the time. But that's not clear from the code snippet.

Replies are listed 'Best First'.
Re^5: what is sub {1;}
by moritz (Cardinal) on May 27, 2008 at 14:14 UTC
    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?

    Because that way my code stays maintainable. You already have two blocks of nearly identical code, and if the call to $filter comes up in different places, you duplicated code for each of these occurrences.

    If my code is too slow I can still go back and profile and optimize it.

    But duplicate code kills maintainability, because if you fix your code in one place you can be sure you forget to fix it in some other cases.

    My example was reduced to the bare minimum that looked useful, but more often the actually duplicated code is larger.