Hi all

I noticed a couple of questions earlier about modules. I have only been at
Perl a short while and when I first encountered them I was able to relate to
them as libraries in C/C++. 

However, this analogy does not go any distance to help the Perl Novice who
is stumbling blindly along the horribly cobbled street that is the road of
the programmer and the first place where his thirst is quenched happens to
be the Brothel run by Perl. I might add its a very good choice of
establishment and although it has a steep learning curve and the beer
sometimes leaves a bitter taste in your mouth when you first taste it. After
a few sips intoxication sets in and it becomes a pleasure where anything's
possible. Unlike an ordinary Brothel though you are not left with a sore
head and an empty pocket in the morning and the impossible things you dreamt
where real, and you are also quite likely to get paid for you stay.  

What I have written is by no means the proper way to write a module or the
only reasons why they are used. I am just detailing my thought process on
the matter but hopefully in reading this some of you similar to me will have
a better understanding of why modules are a good idea and how they can be
used to aid you. It is not a tutorial on how to write a module but more why
to write a module. 


Why Modules.

To be honest I was quite content writing my little Perl scripts that would
take some input perform some magic and create some output. This was more
than enough for me until I decided that I wanted to store all my output in
Excel spreadsheets ( I work in a windows shop ). Due to the nature of the
scripts that I was writing the date was also something that would be written
out to the file each time the script was run ( I was Monitoring users on an
Oracle database ) and I would also need to connect to 13 Oracle databases
dotted all over Europe to do this. For the date I came up with the following
bit of Code. 


sub easytime {
        ($Second , $Minute , $Hour , $Day , $Month , $Year, $Weekday) =
(localtime)[0,1,2,3,4,5,6];
        $Year                           =       ($Year + 1900);
        $Month                          =       ($Month + 1);
        if ($Weekday eq 0){$Weekday     =       "Sunday"        }
        if ($Weekday eq 1){$Weekday     =       "Monday"        }
        if ($Weekday eq 2){$Weekday     =       "Tuesday"       }
        if ($Weekday eq 3){$Weekday     =       "Wednesday"     }
        if ($Weekday eq 4){$Weekday     =       "Thursday"      }
        if ($Weekday eq 5){$Weekday     =       "Friday"        }
        if ($Weekday eq 6){$Weekday     =       "Saturday"      }
        $DMY                            =       "$Day/$Month/$Year";
        $HMS                            =       "$Hour\:$Minute\:$Second";
}  


This is by no means an elegant solution but I am not in any way a Perl Guru
and it seemed to work for me. This meant that every Perl script that I
wanted would have to have this subroutine inserted into it in order for me
to use it and the subroutine would need to be called each time I wanted the
variables updated. I am inherently lazy and decided that I could not be
bothered writing or copying this piece of code into my scripts when I wanted
it so I looked for an alternative solution. I wanted to be able to include
the subroutine in my script but I did not want it taking up 14 lines of code
in the process. 

This led me to Modules so I had a flick through the cookbook and decided
that I would like to get it into a module, I know there are other ways to do
it but I wanted to pick some knowledge up on the way. I eventually came up
with the following.

#!Perl
#

package Harry::EasyTime;

use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
our     ($Second , $Minute , $Hour , $Day , $Month , $Year, $Weekday, $DMY,
$HMS);

use Exporter;
$VERSION = 1.00;

@ISA = qw(Exporter);

@EXPORT         =       qw(&easytime $Second $Minute $Hour $Day $Month $Year
$Weekday);
@EXPORT_OK              =       qw($DMY $HMS);
%EXPORT_TAGS    =       (
                DATE    =>      [ qw($DMY &easytime) ],
                TIME    =>      [ qw($HMS &easytime) ],
                );

sub easytime {
        ($Second , $Minute , $Hour , $Day , $Month , $Year, $Weekday) =
(localtime)[0,1,2,3,4,5,6];
        $Year                           =       ($Year + 1900);
        $Month                          =       ($Month + 1);
        if ($Weekday eq 0){$Weekday     =       "Sunday"        }
        if ($Weekday eq 1){$Weekday     =       "Monday"        }
        if ($Weekday eq 2){$Weekday     =       "Tuesday"       }
        if ($Weekday eq 3){$Weekday     =       "Wednesday"     }
        if ($Weekday eq 4){$Weekday     =       "Thursday"      }
        if ($Weekday eq 5){$Weekday     =       "Friday"        }
        if ($Weekday eq 6){$Weekday     =       "Saturday"      }
        $DMY                            =       "$Day/$Month/$Year";
        $HMS                            =       "$Hour\:$Minute\:$Second";
}
1;


This piece of code got saved in my /perl/lib directory as Harry/EasyTime.pm.

Now when I wanted to use the subroutine I could insert "use
Harry::EasyTime;" at the top of the script and when I wanted to get an
update to the variables I could call the subroutine "easytime()" from any
where in the program for an update to my time which meant the that variables
below that got exported could be used globally. I know this is also not
ideal but I was after a quick fix.

$Second , $Minute , $Hour , $Day , $Month , $Year, $Weekday, $DMY, $HMS 

By the time I finished the Module I had a better understanding of Namespace,
local variables etc and it was a good exercise in Perl. I then applied my
new found knowledge to connecting to multiple Oracle databases to fetch
results and creating Intersects and Unions from arrays. What I am able to do
in what appears to be a few lines of code now seems like magic compared to
the bulky programs I wrote before.


What I have written is not ideal or the correct way to write these things
but I hope that anyone new to Modules can now see a use for them. If not
read on.


Another reason why modules are used is that programmers are lazy and that's
a fact. Why reinvent the wheel especially when the wheel that you invent may
look more like a fifty pence piece (For those in different countries its a
hexagonal type shape but all its points sit on the circumference of a
circle. Are you confused yet). What I mean is, some of the best minds in the
business have done a lot of work on how to get jobs done in an efficient,
safe and secure manner and have created modules to enable other users to
reap the benefits of their work.

I am not about to write a module that helps me write a web page. The reason
for this is two fold, I do not have the knowledge to do a decent job of it
and get it write and someone much smarter than me has already done it and
done a great job. 

Harry

Disclaimer
For the same reasons as given above I am not about to write a tutorial on
writing Modules. I am not clever enough and there are people out there who
have done a better job than any attempt I would make. Please do not use the
code above as a good example on how to write a Perl Module. I am stating now
categorically that it is not a good example. However, I do think that it may
present a valid reason on why you may want to write a Perl Module. To see
examples on how not to write a Perl Module I may be able to provide various
fantastic examples of this with wild abandon but I do not want to become one
of the wicked.
 "So it shall be at the end of the world: the angels shall come forth, and
sever the wicked from among the just, and shall cast them into the furnace
of fire: there shall be wailing and gnashing of teeth" (Matthew 13:49-50). 












*************************************************************************************
COLT Telecommunications
Registered in England No. 2452736
Registered Office: Bishopsgate Court, 4 Norton Folgate, London E1 6DQ
Tel. +44 20 7390 3900

This message is subject to and does not create or vary any contractual
relationship between COLT Telecommunications, its subsidiaries or 
affiliates ("COLT") and you. Internet communications are not secure
and therefore COLT does not accept legal responsibility for the
contents of this message.  Any view or opinions expressed are those of
the author. The message is intended for the addressee only and its
contents and any attached files are strictly confidential. If you have
received it in error, please telephone the number above. Thank you.
*************************************************************************************


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to