Search code examples
perlobjectprocedural

Perl design question


What is the difference between writing sub functions and putting it all in one file vs. writing packages? Is object oriented better than procedural when it comes to Perl?

Basically looking for examples of scenarios where OO is better than procedural.

Thanks!


Solution

  • First just to clarify, the difference between procedural and OO is not the same as the difference between putting all of your code in one file versus putting it into separate modules. You can have separate modules that are full of functions that you call procedurally.

    Where using modules, OO or procedural, is advantageous is if the code is going to be reused or if it is just a large code base. If you have a CMS with 10 different CGI scripts that all do several of the same things, such as verifying a user session perhaps, then putting that code into a separate module rather than rewriting it in every CGI makes sense. If it's a 20 line function specific to that script, then leave it in the same file.

    Whether to go with OO or procedural depends on what you are doing. Most people are going to favor OO the majority of the time these days. I agree with them as I feel it helps you think about your code logically and group things together in a sane way that will be easy to manage and update later on.