develooper Front page | perl.beginners | Postings from December 2002

Re: Closure definition (was: Passing array to a function)

Thread Next
From:
wiggins
Date:
December 19, 2002 13:11
Subject:
Re: Closure definition (was: Passing array to a function)
Message ID:
200212192111.PAA06466@crows.siteprotect.com

------------------------------------------------
On Thu, 19 Dec 2002 17:44:22 +0100, "Jenda Krynicky" <Jenda@Krynicky.cz> wrote:

> From: wiggins@danconia.org
> > ------------------------------------------------
> > On Thu, 19 Dec 2002 11:04:27 -0500, Satya_Devarakonda@tufts-health.com
> > wrote:
> > > But is it not wrong to create an array in a function and send a
> > > reference to the array back into the main function?
> > > 
> > > Because the scope of the array is limited to the function and
> > > calling a reference that is not available(the variables local in
> > > scope to the function are collapsed) must be illegal!!
> > > 
> > > Regards,
> > > Satya
> > 
> > You have just learned what a closure is :-).  At least that is my
> > understanding of one.  
> 
> No this is not a closure.
> 
> This is:
> 	sub createClosure {
> 		my $x = shift;
> 		return sub { print $x++,"\n" }
> 	}
> 
> 	$closure = createClosure( 5 );
> 	$closure->();
> 	$closure->();
> 
> Closure is a function that references a variable lexical to the scope 
> it was created in even though the scope is long gone.
> 
> Jenda


Obviously I never did quite get that.  But your explanation makes sense to me. So it is similar to what he had only rather than it being a reference directly (aka as a scalar) to the other value, it is a reference to a subroutine that then accesses the value?

http://danconia.org

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About