perl closure 闭包
# cat closure.pl#!/usr/bin/perl -w
use strict;
sub outer {
my $color = shift;
my $ref = sub {
my $object = shift;
print "paint the $object $color.\n";
};
return $ref; # returns a closure.
}
my $p1 = outer("red");
my $p2 = outer("blue");
$p1->("flower");
$p2->("sky");
# perl closure.pl
paint the flower red.
paint the sky blue.
页:
[1]