Perl - Library


A large part of any coding project will involve snippets which are copied over and used at multiple places. If we identify these snippets and separate out in some manner, this would serve two useful purpose: 
  • Easier way to access this reusable code.
  • Easier way to fix issues in this common code.
Perl allows creation and use of libraries to aid code reuse. 

Let us take an example of below script: 

use strict;
use warnings; 

sub add {
    my $res = 0;  
    while(@_) {
        $res += shift;
    }   
    return $res;
};

sub multiply {
    my $res = 1;  
    while(@_) {
        $res *= shift; 
    }   
    return $res; 
}

print add(4, 8), "\n";
print multiply(4, 8), "\n";

The above example implements two subroutines "add" and "multiply". There is huge possibility that these routines will be used across multiple scripts. As such, placing them in a common place would make sense.