Possible Duplicate:
How do you create a Perl module?
I have the script that reads an xml file and creates hash table. its working properly but now i need to create module for that code, that i can call in my main function.In my main function file path as input and it gives output as hash. now i need to create module for this code.
#!/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\n" );
print( " Datainfo: $des\n" );
}
how can i create module for this code?
Take a look at the Perl Documentation. One of the tutorials included is perlmod. This offers a lot of good information.
First step: Make your program into a subroutine. That way, you can call it your code. I’ve taken the liberty to do that:
I’ve made a wee change. You notice that I no longer open a file. Instead, you’ll pass a file handle to your
MyFunctionsubroutine. Second, instead of printing out$desnodeand$des, it now returns a hash that has these values in them. You don’t want subroutines to output data. You want them to return the data, and let your program decide what to do with the information.I’ve also put in a
use Carp;line. Carp gives you two functions (as well as a few others). One is calledcarpwhich is a replacement forwarning, and the other is calledcroakwhich is a replacement fordie. What these two functions do is report the line number in the user’s program which called your function. That way, the user doesn’t see the error in your module, but their program.I’ve also added the line
1;at the bottom of your program. When a module loads, if it returns a zero on load, the load fails. Thus, your last statement should return a non-zero value. The1;guarantees it.Now that we have a subroutine that you can return, let’s make your program into a module.
To create a module, all you have to do is say
package <moduleName>on top of your program. And, also make sure that the last statement executes with a non-zero value. The tradition is just to put a1;as the last line of the program. Modules names end with a.pmsuffix by default. Modules names can have components in the names separated by double colons. For exampleFile::Basename. In that case, the module,Basename.pmlives in the directoryFilesomewhere in the@INClist of directories (which, by default includes the current directory).The
packagecommand simply creates a separate namespace, so your package variables and functions don’t collide with the names of the variables and functions inside the program that uses your package.If you use an object oriented interface, there’s no reason why you need to export anything. The program that uses your module will simply use the object oriented syntax. If your module is function based, you probably want to export your function names into the main program.
For example, let’s take
File::Basename. This module imports the functionbasenameanddirnameinto your program. This allows you to do this:Instead of having to do this:
To export a function, make sure your module uses the
Exportermodule, and then set the package variable@EXPORT_OKor@EXPORTto contain the list of functions you’re allowing to be exported in the user’s program. The difference is that if you say@EXPORT_OK, the functions will be exported, but the user must request each one. If you use@EXPORT, all those functions will automatically be exported.Using your program as a basis, your module will be called
Mypackage.pmand look like this:The big thing is the use of:
package Mypackageuse Exporter qw(import)our @EXPORT_OK qw(myFunction);The
packagefunction sets up an independent name space, so your variables and function names don’t override (or get overwritten) by the user’s program.The
use Exportersays that your program is using theimportfunction of theExportermodule. This allows you to import variables and functions into the main namespace of the user’s program. That way, the user can simply refer to your function asmi_function_nameinstead ofMypackage::my_function_name. In theory, you don’t have to export anything, and newer modules don’t. These module are entirely object oriented or just don’t want to bother with namespace issues.The
@EXPORT_OKarray says what you’re exporting. This is preferred over@EXPORT. With@EXPORT_OK, the developer must specify what functions he wants to import into their program. With@EXPORT, this is done automatically.In the program that uses your module, you’ll need to do this:
Now, all you have to do in your program is
Now, things are constantly evolving in Perl, and I’ve never received any formal training. I simply read the documentation and take a look at various examples and hope that I understand them correctly. So, if someone might say that I’m doing something wrong, they’re probably correct. I’ve also didn’t test any of the code. I might have screwed something in your program when I turned it into a subroutine.
However, the gist should be correct: You need to make your code into callable subroutines that return the information you need. Then, you can turn it into a module. It’s not all that difficult to do.