Making Your Own Libraries: Perl Modules
#!/usr/bin/perl -w
###########################  Module Demonstration  ########################

use lib ("$ENV{HOME}/perl/junk");  # Add to @INC search path array!

# BEGIN {
#       unshift(@INC, "$ENV{HOME}/perl/junk");
#####       }   #### Does same thing as use lib above!!

foreach $dir (@INC)   #  Print out search paths in @INC. 
{
    print "$dir\n";
}
print "\n";

use pkg;            #  In $ENV{HOME}/perl/junk 
use newpkg;         #  In $ENV{HOME}/perl/junk 

pkg::mysub1();      #  You can fully qualify sub calls in packages.
pkg::mysub2();      #  Later, we'll see that this is not usually necessary!

newpkg::newsub1();
newpkg::newsub2();
#################  Do I always need package_name::name??  ###################
#################  Answer: no, unless their are name      ###################
#################  conflicts between multiple packages!   ###################

use string;
$var = "Hello.  I  am Mister    Ed.\n";
$var = clean $var ;    #  Notice -- no qualification of name "clean"!
$var = encrypt $var;   #  Notice -- no qualification of name "encrypt"!
print "\n$var\n";
####################  End of Module-Using Program  #######################
#######################  Pkg.pm Contents Below  ##########################
package pkg;  

use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(&mysub1 &mysub2);  #  &'s are purely cosmetic.

sub mysub1  { print "Pkg mysub1 function.\n"; }
sub mysub2  { print "Pkg mysub2 function.\n"; }
1;      #  All modules MUST end with a true expression -- hence 1; 
########################  Newpkg.pm Contents Below  ###########################

package newpkg; 

use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(&newsub1 &newsub2);  ######  &'s are purely cosmetic.

sub newsub1 { print "Newpkg newsub1 function.\n"; }
sub newsub2 { print "Newpkg newsub2 function.\n"; }
1;             
##########################  String.pm Contents Below  #########################
package string;

use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(&clean &encrypt);  ######  &'s are purely cosmetic.

sub clean
{
    my ($string) = @_;

    $string = uc($string);

    $string =~ s/^\s*(.*?)\s*$/$1/;
    $string =~ s/\s+/ /g;
    return $string;
}


sub encrypt
{
    my ($string) = @_;
    my ($result, $char) = ("");

    foreach $char (split //, $string)  #  Empty split delimiter!
    {
         $result .= chr(ord($char) + 2) unless $char =~ /\s/;
         $result .= $char if $char =~ /\s/;
    }
    return $result;
}
1;
###########################  Program Output Below  ############################

/mnt/diska/staff/jwp2286/perl/junk     #  Here's the search paths in @INC.
/usr/local/perl/lib/IP19-irix/5.00404
/usr/local/perl/lib
/usr/local/perl/lib/site_perl/IP19-irix
/usr/local/perl/lib/site_perl
.    #  Notice that current directory is always in @INC.

Pkg mysub1 function.
Pkg mysub2 function.
Newpkg newsub1 function.
Newpkg newsub2 function.

JGNNQ0 K CO OKUVGT GF0    #  My clean'ed, encrypt'ed string!