Sunday, February 8, 2009

Simple Perl modules

Making it already a rool to post technical details for which I have spent more than 20 minutes, I decided to post as well this.

keywords: How to write perl modules

Answer: it's simple!

Create file in Lib/ directory where you like with the following contents:

package Lib::StringManip;

use strict;

use base 'Exporter';
our @EXPORT = ('trim');

# Perl trim function to remove whitespace from the start and end of the string
sub trim
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;


Add the full path to Lib/ to PERL5LIB environment variable. In my case (win32) it is: PERL5LIB=%PERL5LIB%;D:\Programming\Perl

i.e. inside D:\Programming\Perl I have Lib/ In Linux/Unix: export PERL5LIB=some_path/Lib/

Usage snippet:

#!perl -w

use strict;
use Lib::StringManip;

print trim(" trim me! ");

No comments: