中国网络水泥 发表于 2018-8-31 09:44:03

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]
查看完整版本: perl closure 闭包