Sunday, November 12, 2006

creating your own perl module

we are going yo create a simple perl module.  Good thing is there are tools shipped with perl that generate stub code. Lets say we want to generate module called Gloo::Store. To generate the stub issue the command $h2xs -AXc -n Gloo::Store. This would create a folder called Gloo-Store. Your module Store.pm would be located in Gloo-Store\\lib\\Gloo folder. I will skip over lot of details.

The intention is to just add two methods to our module that we can use in our other scripts.  we have added a method to this module called save that uses CGI::Session module to store data. The extra code we wrote is in color red. The stub generated code is in color black.  You can have your documentation right inside the module. All the documentation is in color blue. And finally we do not export any method names because we are using the OO interface.

package Gloo::Store;
use 5.008007;
use strict;
use warnings;
use CGI ;
use CGI::Session ;

require Exporter;

our @ISA = qw(Exporter);
our %EXPORT_TAGS = ( 'all' => [ qw( ) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw();
our $VERSION = '0.01';

sub new {
  my $package = shift;
  return bless({}, $package);
}

sub save {
    my $shelf = shift ;
    my ($cgi, $token, $value) = @_ ;
    if( !defined $token || $token eq "" || !defined $value || $value eq "") {
        return ;
    }
    print " $token :: $value \n " ;
    # @todo - change this location
    my $session = new CGI::Session(undef, $cgi,{Directory=>"c:/rajeev/perl/tmp"});
    $session->expire('+15m');
    my $sid = $session->id();
    # put data in  session
    $session->param($token, $value);
    return $sid ;
}

# Preloaded methods go here.
1;

__END__

=head1 NAME
Gloo::Store - Module for storing user preferences of Gloo website
=head1 SYNOPSIS
  use Gloo::Store;
  $gloo = new Gloo::Store ;
  $gloo->save('token','value');
=head1 DESCRIPTION
GlooStore is used to store the gloo user preferences in cgi sessions. The GlooStore module
uses cgi::session module. We require the user to have cookies enabled in their browser. And
no, no cookie monster will eat your computer because we just store the CGI session id in cookies.
=head2 EXPORT
None by default.
=head1 SEE ALSO
CGI::Session
=head1 Methods
=over
=item  save($cgi_handle,$name,$value)
 method to save name value pairs in gloo store.

=cut

=back
=head1 AUTHOR
rajeev jha, E jha.rajeev@gmail.com E
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2006 by rajeev jha

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.7 or,
at your option, any later version of Perl 5 you may have available.
=cut


The usage of this module has been included in the documentation itself. To install this module, go inside the Gloo-Store folder and issue
$perl Makefile.pl
$nmake
$nmake install

O
ur module would be installed to perl site/lib folder.


powered by performancing firefox

© Life of a third world developer
Maira Gall