[sf-perl] alarmingly obscure perl on wikipedia

Kevin Goess cpan at goess.org
Sun Nov 20 01:12:18 PST 2011

On the topic of trying to promote readable, modern Perl, I just happened to
come across this in the wikipedia article on "Function Objects":
http://en.wikipedia.org/wiki/Function_object#In_Perl. It's a terrible,
terrible ambassador for Perl, and I think actually misses the point of the
article. If you compare the Perl code with even the Javascript code it's
downright embarrassing.

I'm going to mirror the Javascript example and rewriting the Perl code as
follows, anybody have any comments on this?

In Perl, functions are first class objects.  Perl also supports closures.

sub make_accumulator {
    my $current = shift;
    return sub { return $current += shift };

$acc1 = make_accumulator(42);
$acc2 = make_accumulator(9000);

print $acc1->(3),   "\n";    # prints '45'
print $acc1->(3),   "\n";    # prints '48'
print $acc2->(542), "\n";    # prints '9542'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/sanfrancisco-pm/attachments/20111120/d85d9c10/attachment.html>

More information about the SanFrancisco-pm mailing list