In Perl
In Perl, a function object can be created either from a class's constructor returning a function closed over the object's instance data, blessed into the class:
package Acc1; sub new { my $class = shift; my $arg = shift; my $obj = sub { my $num = shift; $arg += $num; }; bless $obj, $class; } 1;or by overloading the &{} operator so that the object can be used as a function:
package Acc2; use overload '&{}' => sub { my $self = shift; sub { $num = shift; $self->{arg} += $num; } }; sub new { my $class = shift; my $arg = shift; my $obj = { arg => $arg }; bless $obj, $class; } 1;In both cases the function object can be used either using the dereferencing arrow syntax $ref->(@arguments):
use Acc1; my $a = Acc1->new(42); # prints '52' print $a->(10), "\n"; # prints '60' print $a->(8), "\n";or using the coderef dereferencing syntax &$ref(@arguments):
use Acc2; my $a = Acc2->new(12); # prints '22' print &$a(10), "\n"; # prints '30' print &$a(8), "\n";Read more about this topic: Function Object