in reply to Do file.pl (replacing subs?)

Interesting idea.

I see two problems with it. The first is that while do 'file.pl'; looks neat and tidy, by the time you add the required error checking around each one it is less so and you'll find yourself wanting to wrap the error checking into a subroutine, which sort of defeats the purpose.

The second is that the code will be reinterpreted and compiled each time you need to use it, which would dramatically slow your script down.

You could avoid this by including the contents of the files using require 'file.pl'; but then you'd need to wrap the contents of the file in a subroutine so that you could call it more than once, or you have the same problems of reinterpreting it each time. Once you've wrapped it in a sub, your almost back to where you started, though it would achieve your aims of seperating "implementation from logic", but unless the routines in the files are going to be re-used in multiple programs, I would find it easier to maintain them all in a single file. And if they where to be re-used in multiple files, better to make it a module and use it.


Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!

Replies are listed 'Best First'.
Re: Re: Do file.pl (replacing subs?)
by BUU (Prior) on Sep 23, 2002 at 13:23 UTC
    You have a point for your first two .. points. However, on your last point, the reason i stuck with do vs use was that it seemed visually clearer, so the reader would know that i'm attempting to execute code in a seperate file, as opposed to the common interpretation of use, which is that i'm attempting to import a module. However, use 'file.pl' would have the exact same effect with the benefit of error checking and so forth. Of course, a problem there is if you want to call a 'file' multiple times, i dont think multiple uses would execute the code multiple times.

    You have a point about just wrapping the file in a sub, but that seems to kind of defeat the purpose =/. You're right about the maintence of a whole folder full of subs, but that mostly holds true for local. If you were trying to remotely maintain something, and you improved one sub, you could just send out a new sub-file, and say 'replace file blah in your sub folder with this new file', as opposed to 'look in blah.pl, find line 952-1060 and replace them with this block of code..'

      I wouldn't trust anyone who doesn't know that use loads and imports a module to maintain code.

      I don't see a big distinction between executing code in a separate file and using a module.

      I have no problem sending the whole of an updated module, if necessary.

      Your situation doesn't seem appreciably different from what modules are intended to accomplish.

        >>I have no problem sending the whole of an updated module, if necessary

        Heh, the only real case for this i can think of is say you offer some kind of script, someone downloads it and makes a bunch of modifications. If you upgrade the script (a function in this case) you would wipe out all those mods if you sent the entire script. Of course this is a rather limited idea i suppose and it makes certain assumptions which by no means are always true. But that was basically my reasoning behind it.
      you could just send out a new sub-file, and say 'replace file blah in your sub folder with this new file', as opposed to 'look in blah.pl, find line 952-1060 and replace them with this block of code..'
      diff / patch or rsync anyone? :-)

      Makeshifts last the longest.