Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

RE: RE (tilly) 3: Fly Subroutines on the Fly

by BlaisePascal (Monk)
on Sep 19, 2000 at 22:29 UTC ( [id://33172]=note: print w/replies, xml ) Need Help??


in reply to RE (tilly) 3: Fly Subroutines on the Fly
in thread Fly Subroutines on the Fly

You are right that the example given is not one where a closure would be appropriate, which is why I pointed out the overhead of entering a function.

I think we may have different definitions of "closures". I think of a closure as a subroutine (anonymous or named) that captures the lexical scope of a variable, like the two anonymous divide routines in my code. The original code by Adam and your fix of it don't capture lexical scope; the values of $n are interpolated before eval ever sees it.

You seem to be talking about run-time compilation of new subroutines. While that can be a powerful technique, it is different than closures (to me, at least).

Replies are listed 'Best First'.
RE: RE: RE (tilly) 3: Fly Subroutines on the Fly
by Adam (Vicar) on Sep 19, 2000 at 22:41 UTC
    Exactly. The example I gave is not a closure, no one suggested it was. In fact, I specifically said I was trying this instead of a closure.

    Ok, maybe I wasn't That specific. What I said was, "I'm not sure how to do this using a closure (short of a big ugly if-then-else structure) so I tried to create subroutines..." which I read, perhaps mistakenly, "I didn't like the use of a closure so I tried something else".     I apologize if this confused anyone.

RE: RE: RE (tilly) 3: Fly Subroutines on the Fly
by merlyn (Sage) on Sep 19, 2000 at 22:32 UTC
    You seem to be talking about run-time compilation of new subroutines. While that can be a powerful technique, it is different than closures (to me, at least).
    Yes... casually, people say "closures" when they mean "anonymous subroutines". But properly speaking, a subroutine (anonymous or not) is not a closure unless it also captures lexical state that can go out of scope. See Closures (was Re: for loops) for an example of detection.

    -- Randal L. Schwartz, Perl hacker

      Yes. I tend to be pretty casual.

      In point of fact constructors for interesting anonymous functions almost inevitably are closures. For instance the example I mentioned of something which I would have written this way had I been writing it (Pod::Parser) is one where the optional hooks would be anonymous subs that were in lexical scope. Therefore the final thing constructed is a closure by anyone's use of the language. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://33172]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-03-28 16:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found