Kevin Pfeiffer wrote: > > In article <[EMAIL PROTECTED]>, Rob Dixon wrote: > > > Kevin Pfeiffer wrote: > >> > >> I would have thought that this would initialize my $indent variable to 2 > >> (like setting an initial state for an object), but if I call "indent()" I > >> get nothing back. :-( > >> > >> > >> { # static local variable > >> my $indent = 2; > >> > >> sub indent { > >> my $increment = shift; > >> $indent += $increment if $increment; > >> return $indent; > >> } > >> } > > > > By the way, that's not a closure! It's just a subroutine > > with a non-volatile local variable. > > Hmmm, does the subroutine need to to only exist as a reference to an > anonymous subroutine to be a closure? I'm going by what I read in Learning > Perl Objects, but I may be misinterpreting what I _thought_ I read.
Not strictly no. But that's the way it's conventionally done. What you've written above could be one instance of a closure, but to be useful you need to be able to generate multiple independent instances at run time. Perl lets you add subroutines at run time using 'eval', so that you could compile a set of subroutines with different names instead of using references, but I can't think why you would want to. The usual way of writing a closure generating subroutine which returns an anonymous subroutine together with its own private data is the best way. HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]