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

RE (tilly) 3: Fly Subroutines on the Fly

by tilly (Archbishop)
on Sep 19, 2000 at 15:23 UTC ( #33073=note: print w/replies, xml ) Need Help??


in reply to RE: RE (tilly) 1: 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 don't have the energy to write a real example to show you what I mean, but I can point at a case which would have been helped by it. Download Pod::Parser and in it look for the implementation of parse_paragraph. There are a lot of optional hooks provided, and the way it was done is to pass all of the data through a series of functions then keep on checking flags. But most of the time those functions (like preprocess_line and preprocess_paragraph) don't do anything.

I once looked at it closely, and couldn't tie out the current API. But if you had an API where you created all of the hooks you wanted then asked it to build a parser, it could then create a function, and then once (and once only) decide to only include lines with optional hooks if you were using it. The result would do the same task several times faster since optional hooks would not be present in the final output. (Without having looked I would suspect that Parse::RecDescent does this.)

As for why the eval, to do what I describe you would want to build up the string, as you walk through checking once for the hook then including or not including bits of what could be part of your sub, then call eval to have it return the final subroutine for you.

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

Replies are listed 'Best First'.
RE: RE (tilly) 3: Fly Subroutines on the Fly
by BlaisePascal (Monk) on Sep 19, 2000 at 22:29 UTC
    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).

      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.

      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://33073]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2023-09-22 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?