Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris


by lorenw14 (Initiate)
on Apr 16, 2001 at 04:36 UTC ( #72718=perlquestion: print w/replies, xml ) Need Help??
lorenw14 has asked for the wisdom of the Perl Monks concerning the following question:

It is possible to make references to lexical scalar, array, and hash variables. These lexical references can be "closed over" by subroutines. What does this mean?

Replies are listed 'Best First'.
Re (tilly) 1: References
by tilly (Archbishop) on Apr 16, 2001 at 07:11 UTC
    Yes, you definitely want to know about closures.

    The interesting thing about closures is that you hear about them and wonder what they could possibly be good for. Then you run across someone who is using them and you wonder how they thought up the crazy stuff they are doing. Then a switch goes off somewhere inside and you suddenly have no trouble figuring out interesting things to do with them.

    So hopefully you have now heard about them and are wondering what they could possibly be good for. Well now try this, that, or the solution I threw together for 5x5 Puzzle for some samples to move you to the next stage...

(redmist) Re: References
by redmist (Deacon) on Apr 16, 2001 at 05:15 UTC

    I can't tell you what it means, but I can give you some suggestions on how to make better posts:

    • Read the Perl Monks site FAQ
    • Wrap your code in <code> tags (per your first post)
    • Provide us with some context and some more code, if you could.

    Silicon Cowboy
(sacked: perlman:perlsub) Re: References
by sacked (Hermit) on Apr 16, 2001 at 05:20 UTC
    from perldoc -f my:
    my EXPR : ATTRIBUTES A `my' declares the listed variables to be local (lexically) to the enclosing block, file, or `eval'. If more than one value is listed, the list must be placed in parentheses. See the Private Variables via my() entry in the perlsub manpage for details.
    You can find the aforementioned reference here: perlman:perlsub.

    Update: My apologies, I misread the question and thought lorenw14 was asking about the scope of lexical variables.

Re: References
by lachoy (Parson) on Apr 16, 2001 at 06:40 UTC

    It sounds like you're interested in a closure, which is basically a function that has its own set of private data it carries around. (Or the opposite of most OO programing, which is a piece of data that carries around functions.) There's a quite good explanation of this in the Perl documentation -- perldoc -q closure brought it up for me, or you can check out perldoc perlref for lots of reference goodies.

    M-x auto-bs-mode

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://72718]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (12)
As of 2018-06-20 17:46 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.