From: "Jon Cassorla" <[EMAIL PROTECTED]> > There are many ways to do this. Here is just one example using a > closure. > > package Remember; > > # this is a closure > { > my $savedData = 0; # only accessible within this scope; > > sub rememberData > { > my $class = shift; > $savedData = shift if @_; # update with new value if given > return $savedData; # return current value > } > } > > 1;
Sorry ... this is NOT a closure. I don't think this trick has a name. This is a closure : sub makeGreeter { my $greeting = shift; return sub {print $greeting,"\n";} } $hello = makeGreeter ( "Hello world" ); $hi = makeGreeter ( "Hi dude" ); print "No print the greeting:\n"; &$hello(); &$hi(); >From perlfaq7 : *Closure* is a computer science term with a precise but hard-to-explain meaning. Closures are implemented in Perl as anonymous subroutines with lasting references to lexical variables outside their own scopes. These lexicals magically refer to the variables that were around when the subroutine was defined (deep binding). Closures make sense in any programming language where you can have the return value of a function be itself a function, as you can in Perl. Note that some languages provide anonymous functions but are not capable of providing proper closures: the Python language, for example. For more information on closures, check out any textbook on functional programming. Scheme is a language that not only supports but encourages closures. Jenda =========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ========== There is a reason for living. There must be. I've seen it somewhere. It's just that in the mess on my table ... and in my brain. I can't find it. --- me -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]