Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6685191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:01:47+00:00 2026-05-26T05:01:47+00:00

Possible Duplicate: How do you create a Perl module? I have the script that

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:01:48+00:00Added an answer on May 26, 2026 at 5:01 am

    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:

    #!/usr/bin/perl
    use warnings;
    use strict;
    use Carp;
    use XML::LibXML::Reader;
    
    #Reading XML with a pull parser
    sub myFunction {
        my $fh = shift; #File Handle (should be opened before calling
    
        my $reader = XML::LibXML::Reader->new( IO => $fh )
            or croak ("unable to open file");
        my %nums;
        while ($reader->nextElement( 'Data' ) ) {
    
            my $des = $reader->readOuterXml();
    
            $reader->nextElement( 'Number' ); 
            my $desnode = $reader->readInnerXml(); 
    
            $nums{$desnode} = $des;
        }
        return %nums;
    }
    
    1;
    

    I’ve made a wee change. You notice that I no longer open a file. Instead, you’ll pass a file handle to your MyFunction subroutine. Second, instead of printing out $desnode and $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 called carp which is a replacement for warning, and the other is called croak which is a replacement for die. 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. The 1; 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 a 1; as the last line of the program. Modules names end with a .pm suffix by default. Modules names can have components in the names separated by double colons. For example File::Basename. In that case, the module, Basename.pm lives in the directory File somewhere in the @INC list of directories (which, by default includes the current directory).

    The package command 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 function basename and dirname into your program. This allows you to do this:

    my $directoryName = dirname $fileName;
    

    Instead of having to do this:

    my $direcotryName = File::Basename::dirname $fileName;
    

    To export a function, make sure your module uses the Exporter module, and then set the package variable @EXPORT_OK or @EXPORT to 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.pm and look like this:

    #!/usr/bin/perl
    
    package Mymodule;
    
    use warnings;
    use strict;
    use Exporter qw(import);
    use Carp;
    use XML::LibXML::Reader;
    
    our @EXPORT_OK(myFunction);
    
    #Reading XML with a pull parser
    sub MyFunction {
        my $fh = shift; #File Handle (should be opened before calling
    
        my $reader = XML::LibXML::Reader->new( IO => $fh )
            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;
        }
        return %nums;
    }
    
    1;
    

    The big thing is the use of:

    • package Mypackage
    • use Exporter qw(import)
    • our @EXPORT_OK qw(myFunction);

    The package function 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 Exporter says that your program is using the import function of the Exporter module. 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 as mi_function_name instead of Mypackage::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_OK array 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:

    use Mypackage qw(myFunction);
    

    Now, all you have to do in your program is

    my %returnedHash = myFunction($fh);
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Create a date with T-SQL I've a data table that stores each
Related/Possible duplicate: How can I validate dates in Perl? I have created a script
Possible Duplicate: How do I create a hash of hashes in Perl? I need
Possible Duplicate: Create a UIViewController that contain a UIViewController I have a parent view
Possible Duplicate: Create a variable in .CSS file for use within that .CSS file
Possible Duplicate: Create Excel file in Java How to save output in excel format
Possible Duplicate: How do I create unique constraint that also allows nulls in sql
Possible Duplicate: How do you create an application shortcut (.lnk file) in C# or
Possible Duplicate: How can I create a temp file with a specific extension with
Possible Duplicate: How to create my own JavaScript Random Number generator that I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.