keywords: How to write perl modules
PageRank: http://www.netalive.org/tinkering/serious-perl/
Answer: it's simple!
Create file StringManip.pm 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;
}
1;
Add the full path to Lib/StringManip.pm to PERL5LIB environment variable. In my case (win32) it is: PERL5LIB=%PERL5LIB%;D:\Programming\Perl
i.e. inside D:\Programming\Perl I have Lib/StringManip.pm. In Linux/Unix: export PERL5LIB=some_path/Lib/StringManip.pm.
Usage snippet:
#!perl -w
use strict;
use Lib::StringManip;
print trim(" trim me! ");
No comments:
Post a Comment