Should I change my utilities.pl to the utilities.pm module?

In our product, we have a large utility file that we need (with do) at the beginning of a large number of our files. Is there any reason not to turn this into a module? For example, instead:

do '../dbi_utilities.pl'; our ($db,$user,$pw,$attr); my $Data = DBI->connect($db,$user,$pw,$attr) or die "Could not connect to database: $DBI::errstr"; 

Can't I just do it ?:

 use AppUtil; my $Data = AppUtil->connect(); 

What are the pros and cons of each style?

+6
module perl use
source share
5 answers

The only reason not to do this is time.

Thus, it will take time to clean up your interface, as well as all calling applications, to use the new interface.

What will cost you on time will be more than compiled when you start using the right tests ("do a test" or "./Build test" or just "prove ...") and you can verify that your changes will not break anything, before checking it out. So, be sure to transform. Just keep in mind that this is not a free win.

+8
source share

By creating your code in a module with proper refactoring, you can easily test it. I talk about this in “Scripts as Modules” for Perl Magazine , as well as “How Script Becomes a Module” in Perlmonks.

Good luck

+7
source share

With do (), you download and compile the utilities.pl file each time, which can cause problems if you make () it more than once. In addition, use is executed at compilation time, which will allow your program to crash earlier or even be tested with perl -wc .

Finally, storing it in a package helps protect its namespace, which can be useful as your project grows.

I would highly recommend turning your utilites.pl into the correct Perl package, which is loaded using use .

+6
source share

Creating a module from it will make it much more reliable. Now many things informally depend on each other, but these dependencies are not immediately obvious.

In addition, it will allow you to import only part of the utilities.

+2
source share

You get all the cool module material, encapsulation, module-specific functions, etc.

Note that using use with your syntax. Create an object for the AppUtil namespace and call the connect routine. for your utilities.

You should also have 1; at the end of your file.


Using a different method means that you do not need to change any code, you do not need to add 1 to the end.

All "do", "use" and "require" import, but the code of the area that is inside them (except for the named routines, because they cannot be hidden).

+1
source share

All Articles