Closure in action : Closure « Subroutine « Perl






Closure in action

   

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");

   
    
    
  








Related examples in the same category

1.A closure is an anonymous subroutine
2.Using closures.