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.h...e%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:
Code:
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\n" );
print( " Datainfo: $des\n" );
}
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
Code:
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\n" );
print( " Datainfo: $des\n" );
}
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 :
Code:
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).
Bookmarks