programming-examples/perl/Subroutine/Closure in action.pl

14 lines
286 B
Perl
Raw Normal View History

2019-11-15 12:59:38 +01:00
sub paint {
my $color = shift;
my $ref = sub {
my $object=shift;
print "Paint the $object $color.\n"; # $color still in scope
};
return $ref;
}
my $p1=paint("red");
my $p2=paint("blue");
$p1->("flower");
$p2->("sky");