Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
What are "delegates"? Talking about C#?

Most things which can be done with closures can be done with OOP and vice versa.

I normally prefer closures because it's light weighted and works in different languages alike.

just a very simple example of encapsulation.

{ my $var; sub getter { $var }; sub setter { $var = shift }; }

(EDIT: Applied in Perl OO one gets private class-variables in package scope instead of public package variables!)

In JS which "only" has prototype-kind of OO, it's possible to simulate many "classical" OO-models just by applying closure extensions. (That is having a "real" class as pattern to construct object instances.)


Cheers Rolf


OK people, please lets get the terminology right:

Closures are NOT synonymous for

  • anonymous functions,
  • nor for currying
  • nor do they need to be produced dynamically by a generator function!

Closures are functions which access closed over variables from an (normally restricted) outer scope at definition time.

Look at the example I gave, getter and setter are closures, $var is a closed over lexical variable, and because of the surrounding curlies the variable is only accessible by those closure functions.

No currying, no anonymous functions no dynamic generation!!!

Most of you use them every day!!!

Whenever you define a file-scoped lexical variable then all functions within this file accessing this variable are closures!

In reply to Re: Real life uses for closures. (update: disambiguation) by LanX
in thread Real life uses for closures. by BrowserUk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (7)
    As of 2018-03-23 02:21 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (287 votes). Check out past polls.