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\
" );
}
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).
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.