Re: advanced: closures (was Re: test for real number)

2001-05-31 Thread Peter Scott

At 10:51 AM 5/31/01 -0700, Randal L. Schwartz wrote:
>There's a school of (mis-)thought that "a coderef is a closure".  I
>don't know how that got started, but it's wrong.  A closure is created
>only when lexical variables go out of scope.  So a coderef without
>external references is *not* a closure, and a named subroutine *can*
>be a closure.

Exactly.  To quote Damian Conway's "Object-Oriented Programming with Perl", 
p. 56:

"To hear some people talking about closures, you'd think they were 
discussing quantum physics, brain surgery, or VCR programming.  In reality, 
the idea of closures is incredibly simple and obvious, once the technical 
jargon has been stripped from it.

"In Perl, a closure is just a subroutine that refers to one or more lexical 
variables declared outside the subroutine itself."

Perhaps we've exposed the beginners to enough of the seamy underbelly of 
Perl experts disagreeing among themselves, assuming any are still following 
this thread...

--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com




Re: advanced: closures (was Re: test for real number)

2001-05-31 Thread Randal L. Schwartz

> "Jeff" == Jeff Pinyan <[EMAIL PROTECTED]> writes:

Jeff> I was scolded highly about that by Uri, back when my article[1] about
Jeff> closures.  I, too, felt that foo() should be called a closure, but Uri
Jeff> seemed so convincing.  If you're going to let me say that functions that
Jeff> refer to lexical variables in such a fashion are closures, thank you, I
Jeff> can return my normal "sane" self.

Uri is occasionally confused. :-)

There's a school of (mis-)thought that "a coderef is a closure".  I
don't know how that got started, but it's wrong.  A closure is created
only when lexical variables go out of scope.  So a coderef without
external references is *not* a closure, and a named subroutine *can*
be a closure.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Re: advanced: closures (was Re: test for real number)

2001-05-31 Thread Jeff Pinyan

On May 31, Randal L. Schwartz said:

>> "Jeff" == Jeff Pinyan <[EMAIL PROTECTED]> writes:
>
>Jeff> A closure is an ANONYMOUS function (constructed via $x = sub {
>Jeff> ... }) that contains LEXICAL variables that have been defined in
>Jeff> a scope visible to the closure itself.
>
>leave out the word ANONYMOUS there.
>
>ANONYMOUS and CLOSURE are orthogonal.
>
>in "BEGIN { my $x; sub foo { ... $x ... } }", foo is a CLOSURE
>and is not ANONYMOUS.

I was scolded highly about that by Uri, back when my article[1] about
closures.  I, too, felt that foo() should be called a closure, but Uri
seemed so convincing.  If you're going to let me say that functions that
refer to lexical variables in such a fashion are closures, thank you, I
can return my normal "sane" self.


[1] "Using Closures", by Jeff Pinyan, May 2000:
http://www.pobox.com/~japhy/articles/pm/2000-05.html

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
**I no longer need a publisher for my Perl Regex book :)**




Re: advanced: closures (was Re: test for real number)

2001-05-31 Thread Randal L. Schwartz

> "Randal" == Randal L Schwartz <[EMAIL PROTECTED]> writes:

> "Jeff" == Jeff Pinyan <[EMAIL PROTECTED]> writes:
Jeff> A closure is an ANONYMOUS function (constructed via $x = sub {
Jeff> ... }) that contains LEXICAL variables that have been defined in
Jeff> a scope visible to the closure itself.

Randal> leave out the word ANONYMOUS there.

Randal> ANONYMOUS and CLOSURE are orthogonal.

Randal> in "BEGIN { my $x; sub foo { ... $x ... } }", foo is a CLOSURE
Randal> and is not ANONYMOUS.

In fact, a closer definition is probably:

A closure is a subroutine that would have persisted beyond the
lexical variables it uses, but instead extends the lifetime of
those variables to at least match its own.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Re: advanced: closures (was Re: test for real number)

2001-05-31 Thread Randal L. Schwartz

> "Jeff" == Jeff Pinyan <[EMAIL PROTECTED]> writes:

Jeff> A closure is an ANONYMOUS function (constructed via $x = sub {
Jeff> ... }) that contains LEXICAL variables that have been defined in
Jeff> a scope visible to the closure itself.

leave out the word ANONYMOUS there.

ANONYMOUS and CLOSURE are orthogonal.

in "BEGIN { my $x; sub foo { ... $x ... } }", foo is a CLOSURE
and is not ANONYMOUS.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!