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

I'm using Apache/Mod_perl to run our local intranet site. My server (solaris 2.6) has only 128Mb ram and a poor old Sparc so I'm concerned about performance.
One of my routines has to call some other Perl scripts on request. I'm worried about using the system() function due to the large overhead. What is the best solution. Is do more efficient? Or even running the script via eval?

Replies are listed 'Best First'.
Re: How to call other programs
by Gloom (Monk) on Feb 01, 2001 at 14:51 UTC
    do() is really more efficient than system().
    There's no new process creation with do and it's a key avantage for your problem ( process creation is time/ram/cpu consuming ). Yours do'ed scripts share global vars with your main script ( this should be usefull also ).

    Hope this helps :)

      Thanks Gloom, pretty much confirming what I suspected.
Re (tilly) 1: How to call other programs
by tilly (Archbishop) on Feb 01, 2001 at 20:23 UTC
    I should note some gotchas. First of all if the scripts in question call exit, your script exits as well. Secondly if the scripts in question play with global variables, you could have your namespace messed up with at all.

    Having had the misery of maintaining Perl written as if it was shell, I strongly recommend taking the functionality in those scripts and moving it into modules, then rewriting both the individual scripts and your new programs to load the modules and call them.

    Trust me, I have been there, done that, don't recommend the scars...

      Thanks for that Tilly. I've already got to review the called scripts to trap any 'Variable "$foo" will not stay shared' warnings (the current bane of my life), I'll add exit to the list.
        If I am not mistaken, $foo will not stayed shared is a sign that they are using as globals the same thing that you declared to be lexical. That can be fixed by moving the functionality of how you do a call into its own module that doesn't have any lexicals.

        But have you trapped the case where they are using the same global variables as each other?

        You can fix that, sort of, by wrapping your do in an eval that puts each invocation into a private package. Of course if they use package also, you are out of luck again on the global issue. Likewise if they sometimes refer to variables as being explicitly in package main. Not to mention the joys of global signal handlers.

        Please trust me when I say I have the scars and don't recommend that route long-term. Refactoring the code into modules is not that hard, and once done you gain a lot of flexibility.

Re: How to call other programs
by arhuman (Vicar) on Feb 01, 2001 at 14:28 UTC
    As far as I know do <file> is strictly equivalent to scalar eval<file>

    I may be wrong but do seems to be better (than system) in term of use of memory...
      I thought do was more efficient, well thats what my Camel tells me anyway :)