Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: what is sub {1;}

by mandarin (Pilgrim)
on May 27, 2008 at 09:00 UTC ( #688628=note: print w/ replies, xml ) Need Help??


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

and what would be the use of such construct?


Comment on Re^2: what is sub {1;}
Re^3: what is sub {1;}
by ikegami (Pope) on May 27, 2008 at 09:06 UTC

    It allows

    my $rv = $args{'Post'} ? $args{'Post'}->() : 1;
    to be simplified to
    my $rv = $args{'Post'}->();

    Or if the return value isn't checked, it allows

    $args{'Post'} and $args{'Post'}->();
    to be simplified to
    $args{'Post'}->();

    (sub{} would have been sufficient in the latter case.)

Re^3: what is sub {1;}
by moritz (Cardinal) on May 27, 2008 at 09:11 UTC
    As a default value.

    Suppose you write a sub (or module) that processes some values, and you want to offer the caller the possibility to filter some of them. Now you've got two options:

    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($_); } }
    You can see that the construct with the default value is much shorter (3 instead of ~8 lines), and it's much cleaner.

    If you're interested in more details on callbacks, anonymous subs and many more advanced techniques, I strongly recommend the book "Higher Order Perl" by Mark Jason Dominus.

      # 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.
        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.

Re^3: what is sub {1;}
by DrHyde (Prior) on May 27, 2008 at 09:15 UTC
    It's impossible to say without seeing the rest of the code. I expect that in context it's a sensible default if the user doesn't specify a function himself.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://688628]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-11-27 06:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (180 votes), past polls