| Contents | Up | Prev | Next |
It is possible to define a dynamic reference to a function. The code of its function is written as part of the assignment expression and can see all the variables in the scope in which it was defined. Here's an example:
#!/usr/bin/perl my ($increment); { my $counter; # The definition of a dynamic reference to function comes inside # a "sub {" ... "}" closure $increment = sub { print $counter, "\n"; return $counter++; }; } my $a; while (($a = $increment->()) < 100) { # Do Nothing }
| Contents | Up | Prev | Next |