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

comment on

( [id://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.)

TIMTOWTDI.

Cheers Rolf

UPDATE

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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-19 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found