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

Hello All,

Let me say upfront that I am VERY new to Perl.

I have a large perl program that I have been given to maintain. This perl program is for replicating development environments and does a LOT of stuff. Also it is one large file, not well documented and for lack of a better way of putting it "jumps" around a bit.

My question is this: is there a way of breaking it into smaller chunks for files instead of having everything in one large file? I have done this in other languages and it makes things much more readable.

Sincerely,

Craigbert

  • Comment on How best to break apart a large perl program

Replies are listed 'Best First'.
Re: How best to break apart a large perl program
by davido (Cardinal) on Sep 05, 2013 at 03:57 UTC

    See perlmod, perlobj, perlootut.

    And read Schwern's slides on Skimmable Code (refactoring sanely).

    You may also want to go through Intermediate Perl (O'Reilly), and Modern Perl. They both provide a lot of help on modules, packages, etc. Intermediate Perl may be of particular interest.


    Dave

Re: How best to break apart a large perl program
by kcott (Archbishop) on Sep 05, 2013 at 05:37 UTC

    G'day Craigbert,

    Welcome to the monastery.

    The short answer is yes: you can break up your code into modules, gathering related functionality into separate modules and putting each module into its own file.

    The longer answer is going to depend on what code you're actually dealing with: beyond knowing it's large, "does a LOT of stuff" and "jumps around a bit", we don't have a lot to go on.

    As a start, given you're "VERY new to Perl", I'd suggest you bookmark the Online Perldoc (perl page): possibly read perlintro in the Overview section; look at what's available in the Tutorials section (maybe skimming some of the FAQs); and just make a mental note of the contents of the Reference Manual. Basically, there's far too much to read: get a feel for where things are documented so you can look them up quickly as you need to.

    For creating the modules, take a look at perlmodlib - Modules: Creation, Use, and Abuse, perlmodstyle and perlmod.

    -- Ken

      Hi Ken,

      Thanks for the reply! I was a little vague for two reasons:

      1. I am kind of new to my current job and I am not sure what my current employer would be OK w/my posting and not posting. So as I work through it I may post small chunks for help.

      2. Because I am new I figured (right or wrong) I need to do a massive amount of reading and learning so that I know better what I am doing and can ask better questions here.

      Thanks for the links I will dutifully go through those this weekend. Also, I have purchased used copies of "Learning Perl" (4th ed.) and "Mastering Perl" and I am slowly working my way through those.

      Thanks again for your help...I am sure there will be more questions to come! :)

      Sincerely,

      Craigbert

        When I wrote "we don't have a lot to go on", that wasn't intended as a criticism but rather a reason why I was unable to provide a more detailed answer.

        The code you're working with is more than likely the intellectual property of your employer. There's a good chance that, when you started your job, you entered into some form of legally binding contract in which you agreed not to share that code with third-parties. I'd recommend that you simply do not post that code: I don't know your situation but it's possible that just asking for permission may be viewed negatively.

        A better approach would be to put together minimal, generalised code that's sufficient to explain whatever questions you have and post that. This would be appropriate for module design questions as well as asking for specific "how do I do ...?" help.

        We're more than happy to help, especially when you demonstrate that you've made some effort yourself. Have a look at the guidelines in "How do I post a question effectively?". You'll also find useful information in the "Posting on PerlMonks" FAQ and the "How (Not) To Ask A Question" tutorial.

        -- Ken

Re: How best to break apart a large perl program
by nevdka (Pilgrim) on Sep 05, 2013 at 03:58 UTC

    You'll want to use modules. The files will have a .pm extension, you get a namespace with package, and you use them with a use command in your program. See Simple Module Tutorial for more info.

      Thanks! I will dig into this stuff and go from there!

      Sincerely,

      Craigbert

Re: How best to break apart a large perl program
by Athanasius (Archbishop) on Sep 05, 2013 at 12:31 UTC

    Hello Craigbert, and welcome to the Monastery!

    I would like to add two pieces of advice. First, ensure that the existing code has a good test harness; then, as your refactoring progresses, run your new, refactored code under this same harness to verify that it works exactly the same as the original. You will also find it invaluable to place your work under version control (e.g. using git); that way, when a test fails you can easily roll back the latest changes until you identify the change that introduced the problem. If your existing Perl code doesn’t already have a test harness, look at Test::Tutorial, and use the Test::More module which comes with Perl.

    Second, remember that related code (data and subroutines) should be grouped together into the same module; but separate modules should be loosely coupled to one another (see the Wikipedia article on Loose coupling.) If your modules are loosely coupled, you will be able to work on refactoring one module without having to worry about the impact of your refactoring on the code in other modules (i.e., the rest of the program). Keep these principles in mind as you design the refactoring, and you will find that the work progresses more logically and “naturally”, and that the final product is more easily maintained and extended.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Hi Athanasius, thanks for the reply!

      I have used testing frameworks in the past and have a great appreciation for them. I did not realize that Perl had them (although it seems to have everything else so I should have guessed, right?).

      After I figure out how to better organize the program I will dig through the testing framework links that you have provided.

      I also appreciate the link on loose coupling. I suspect that will be very helpful in the short term.

      Thank you VERY much for your time and thought to my issue.

      Sincerely,

      Craigbert

Re: How best to break apart a large perl program
by pvaldes (Chaplain) on Sep 05, 2013 at 08:41 UTC

    My question is this: is there a way of breaking it into smaller chunks for files instead of having everything in one large file? I have done this in other languages and it makes things much more readable.

    If what you mean is: "a way of breaking it into smaller chunks for files automatically?", yes, you can split your code using a custom record separator (like "\n\nsub "). See the special variables $/ and $\ in: perldoc perlvar. Using this you can pick up chunks of code containing perl objects and print each different function in your code to a different file (function1, function2... use a counter) to be examinated. You will need a little post-processing for fine-tuning probably

      Hi pvaldes,

      My question revolves more around logically breaking it apart as opposed to automatically. Although this looks VERY interesting!

      Thanks for the reply!

      Sincerely,

      Craigbert

Re: How best to break apart a large perl program
by space_monk (Chaplain) on Sep 05, 2013 at 09:00 UTC
    If you're very new to Perl, perhaps your best answer is to dump the entire program on here (minus any security details) and let PerlMonks make suggestions on how it should be broken up as a challenge. After all we have Perl Golf sessions, where the object is to minimise a small program, so why not something more substantial?? :-)
    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

      Thanks for the offer space_monk, I am new to my current place of employment so I am not sure what the boundaries are yet. If it get stuck you can bet that I will certainly post some chunks of it for some Perl Golf sessions! (How COOL is that?!!)

      Thanks!

      Craigbert