EchoAngel has asked for the wisdom of the Perl Monks concerning the following question:

I have two files. One file has the codes I want to append to the end of the function. The second file has the function with a bunch of functions and sub functions. How can I detect the end of a function such that I can add the code to the end of it? ideas?

Replies are listed 'Best First'.
Re: Add Code to functions in File
by jepri (Parson) on May 27, 2004 at 05:00 UTC
    While there are some tricky ways to do this listed above, I'd go for the crude but effective approach of adding comments to the first file, like:

    #---Insert code for function xxx here

    And then search and replace using regexes to put the code in the right place.

    Not tricky, but effective, if you can get those comments in there in the first place.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

      well, i should say that i'm doing this for many files to update the "functions". Another I might have forgotten to message was that most of the files contain data and appear to have function syntaxes.
Re: Add Code to functions in File
by chromatic (Archbishop) on May 27, 2004 at 03:35 UTC

    I'm not sure there's a clean way to do this reliably. Parsing Perl is tricky.

    However, if you're willing to consider a function wrapper, Hook::LexWrap may give you some ideas. Attach a POST handler to the function.

Re: Add Code to functions in File
by revdiablo (Prior) on May 27, 2004 at 03:39 UTC

    You could make your first file -- the one with code you "want to append" -- simply call the functions from the second file, then run some code afterwards. Presumably you have a third file calling the functions in question, so you would change that to use the first file instead of the second.

    Maybe some example code will make more sense:

    I have to wonder why you want to do this, though. It sounds like a sort of fishy request. Perhaps it would be a good idea to look into OO and use inheritance. Perhaps not. Without any details, we can't really give any good suggestions.