in reply to Why are closures cool, continued?
Yes, it is a closure, but it's also an anonymous sub ref, so it's showing multiple things. Here's a much simpler closure:#!/usr/local/bin/perl use strict; my $dave = subref("dave", "oranges", "grapefruit"); $dave->("debug message"); sub subref { my (@stuff) = @_; return sub { doit(@stuff, @_) }; # <-- closure here? } sub doit { print shift, ": ", join(",", @_), "\n"; }
After execution, bar() has a private copy of $foo. TheDamian uses this in his book to do private variables for classes.my $foo; sub bar { $foo++; }
|
---|