From: [EMAIL PROTECTED]
> ------------------------------------------------
> On Thu, 19 Dec 2002 17:44:22 +0100, "Jenda Krynicky"
> <[EMAIL PROTECTED]> wrote:
> 
> > 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?

Yes, that's it. :-)

And just like you can have several references to the same variable 
you can have several closures referencing the same variable:

        sub createClosures {
                my $x = shift;
                return sub {$x}, sub { ++$x }, sub {--$x};
        }

        ($get,$inc,$dec) = createClosures( 5 );
        print $get->(),"\n";
        print $inc->(),"\n";
        print $get->(),"\n";
        print $dec->(),"\n";
        print $get->(),"\n";

Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to