Need help figuring out how to create sharable functions in Perl

I am somewhat new to Perl. I have Googled Perl one liners and worked with the MIME::Lite library to send emails with attachments. But I have not done any real Perl scripting. I need to write a script to install code for our application using an Oracle database with DBI, and to track in the application history table, what was installed and when it was installed. The scripts would then look to see what is already installed every time a patch is run and only install a module once.

Rather than just writing code and repeating a lot of code, I would rather start by building a library of common code that needs to get run and then using the right code when I need it.
I wrote some test code to work with a script that I found on the internet. I am looking for advice on how to make the code modular with reusing as little code as possible. Someone there should be a best practices when it comes to designing how the code should get stored and how to make it modular.

Also what is the difference between require "packageName" and use "packageName" and which one I should use.

Thanks.

#!/usr/bin/perl

# Put on your nerd glasses and draw a square!

use strict;
use warnings;

package myPackage;

sub printSquare {

   my $balancex = 10; # width

   my $repeatx = $balancex; #don't change repeatx!!! use balancex instead.
   my $repeaty = 10; # height

   do{
      while($repeatx > 0){
         print ". "; # change the period to print another character, but keep the extra space.
         $repeatx -= 1;
      }

      print "\n";
      $repeatx += $balancex;
      $repeaty -= 1;
   }  until ($repeaty == 0); #corrected by Athanasius & AppleFritter

}

1;
#!/usr/bin/perl

use strict;
use warnings;

require myPackage;

myPackage::printSquare("printSquare");

Perl reusable code is all about separation of namespace code and careful management of variable scope.

Here are some thoughts on it.

require() function is like #include in C programming, but keeps the lexical scope, meaning that any variable with `my' will not leak to the code that imports it.

It compiles at run time, (compiles just when it about to be used) which has the possibility that it will not run because errors. Perl addresses the issue by requiring that the code imported must evaluate to true, thus the return of 1; at most modules

The syntax:

require "filename"; # a string

or

require Name; # bare word, no quotes (caps in the first letter is a convention) it would look for a module Name.pm

Packages separates subroutine into namespaces. Every subroutine in Perl lives inside a package, every file begins with implicit package main;

__PACKAGE__ will tell you were you are

The syntax:

package package; # name, not quotes

or

package package {...}

In Perl, object oriented and reusable code programming is all about the scope of variables, and subroutines are variables of packages

use Foo;

is equivalent to:

BEGIN {
   require Foo; # we have explained require function before. require can have a string or bareword.
   Foo::import(); # calls the subroutine import() on the package Foo
}

use can only take a bareword, not a string.

A BEGIN code is code that runs at compile time, meaning that as soon as the compiler encounters a BEGIN block, Perl will compile it and run it before even the code that follows is compiled.

1 Like

Hi Aia. Your explanation helps. So can I use the following syntax to make my code aware of code from another file, "use myfile;" then I can call the subroutines from that file, passing in variables that are needed and getting back data? Do I need to specify the package name where I am calling the code from to call a method in the file being included with the use statement?

Do you have any recommendations on what I should look at online to get a clearer idea on how to structure my packages?

Thanks.

It appears to me that what you are craving is how to do object oriented code in Perl. Ultimately, that's what you need to know in order to make use of any code you will find in CPAN, effectively.

I was going to try to compose some examples, but then I thought I knew better not to do that and I got a site for you that without much introduction tries to give you enough material to ponder and further practice.

Perl

The bits about packages and OO is 2/3 down the page.

---------- Post updated at 08:21 PM ---------- Previous update was at 01:22 PM ----------

Maybe this link can be of benefit as well.

How to create a Perl Module for code reuse?

1 Like

Awesome, I will read those fine manuals. This one looks particularly interesting.

"How to create a Perl Module for code reuse?"