Create module in perl

Hi,
I have the xml file and i readed that xml file and i created hash table. now i need to create one module that i can call from main function.in my main function path is the input and it returns hash as the output.so now i need to create one module for my code below,


#!/usr/bin/perl
use warnings;
use strict;
use XML::LibXML::Reader;
#Reading XML with a pull parser
my $file;
open( $file, 'formal.xml');
my $reader = XML::LibXML::Reader->new( IO => $file ) or die ("unable to open file");
my %nums;
while ($reader->nextElement( 'Data' ) ) {
 
my $des = $reader->readOuterXml();
 
$reader->nextElement( 'Number' ); 
my $desnode = $reader->readInnerXml(); 
 
$nums{$desnode}= $des;
print( " NUMBER: $desnode\
" );
print( " Datainfo: $des\
" );
}

Hi Clar

Do you want this as a module or a subroutine ?
Do you want your module to be a package or a class?

http://perldoc.perl.org/perlsub.html discusses how to make a subroutine, which you’d need to know to make a module anyway.
http://perldoc.perl.org/perlmodlib.html#Modules%3A-Creation%2C-Use%2C-and-Abuse discusses module creation.
http://perldoc.perl.org/perlobj.html talks about Perl OO

Let’s first make a subroutine that takes a filename as an argument:


use XML::LibXML::Reader;

sub clar_get_xml_hash {
    my $fnam = shift @_;  ## get the first argument off the parameter stack and use it as the filename.
    my $file;
    open( $file, $fnam) or die ("unable to open file");
    my $reader = XML::LibXML::Reader->new( IO => $file ) ("XML library unable to read file");
    my %nums;
    while ($reader->nextElement( 'Data' ) ) { ## do some XML::libXML stuff
     my $des = $reader->readOuterXml();
     $reader->nextElement( 'Number' ); 
     my $desnode = $reader->readInnerXml(); 
     $nums{$desnode}= $des; ## and make sure it is in the hash.
     print( " NUMBER: $desnode\
" );
     print( " Datainfo: $des\
" );
   }
   close $file; ## be nice and close the file we opened locally.
    return \\%nums; ## return a hash reference.
}
## declare the filename
my $filename="formal.xml";
## use the subroutine to return a hash reference.
## access the data in this hash ref using the format $getnums->{'key'}
my $getnums=clar_get_xml_hash($filename);


if you then want to take that subroutine into a module then put this


package MyModule;

use XML::LibXML::Reader;


sub new { 
## see http://perldoc.perl.org/perlobj.html for an explanation of this code.
    my $this = shift;
    my $class = ref($this) || $this;
    my $self = {};
    bless $self, $class;
    $self->initialize();
     return $self;
}


sub clar_get_xml_hash {
    my $fnam = shift @_;  ## get the first argument off the parameter stack and use it as the filename.
    my $file;
    open( $file, $fnam) or die ("unable to open file");
    my $reader = XML::LibXML::Reader->new( IO => $file ) ("XML library unable to read file");
    my %nums;
    while ($reader->nextElement( 'Data' ) ) { ## do some XML::libXML stuff
     my $des = $reader->readOuterXml();
     $reader->nextElement( 'Number' ); 
     my $desnode = $reader->readInnerXml(); 
     $nums{$desnode}= $des; ## and make sure it is in the hash.
     print( " NUMBER: $desnode\
" );
     print( " Datainfo: $des\
" );
   }
   close $file; ## be nice and close the file we opened locally.
    return \\%nums; ## return a hash reference.
}
1;

into the file MyModule.pm and use it in your code like this :


use strict;
use warnings;

## add the path to your module to the library path to be searched for modules.
use lib 'path to MyModule.pm'; 
use MyModule.pm;

## declare the filename
my $filename="formal.xml";
## declare an object that uses MyModule.pm
my $xmlobj=MyModule->new();

## use the subroutine to return a hash reference.
## access the data in this hash ref using the format $getnums->{'key'}
my $getnums=$xmlobj::clar_get_xml_hash($filename);

Please note : I haven’t tested this code so it hasn’t been debugged.

I’ll leave it to you to extend the MyModule class so the class has the hash or hash reference as a class member You could access the data through the $xmlobj in some way (that you also define).

Please also read http://perldoc.perl.org/Exporter.html
:slight_smile:

Please also read http://perldoc.perl.org/Exporter.html
and add


require Exporter;
  @ISA = qw(Exporter);
  @EXPORT_OK = qw(clar_get_xml_hash);  # symbols to export on request

Let’s create a module called NewModule.pm that doesn’t do very much. I’ll run the h2xs program:

[~/modules],2:05pm% h2xs -AXc -n NewModule
Writing NewModule/NewModule.pm
Writing NewModule/Makefile.PL
Writing NewModule/test.pl
Writing NewModule/Changes
Writing NewModule/MANIFEST
[~/modules],2:05pm% cd NewModule/
[~/modules/NewModule],2:05pm% ls
Changes MANIFEST Makefile.PL NewModule.pm test.pl

The Changes file is where you might keep track keep track of changes you make to your module as you write new versions. If you’re using RCS or CVS version control, you shouldn’t use the Changes file, since all your history & logs will be in revision control and is much more reliable there (you are adding detailed revision notes in version control, aren’t you?). I’ve found that the best scheme is to automatically build the Changes file from the revision control history, but your preferences might vary.

MANIFEST contains a list of files in this directory. If you add new files to the directory, you should also add them to the MANIFEST. The MANIFEST is used to create a tarball of your module for distribution, and it’s also checked when people unpack the tarball and install the module.