veerubiji has asked for the wisdom of the Perl Monks concerning the following question:

hi actuvally i posted how to create module after careful reading i create one module but i have some errors can tel me how to modify

Original content below restored by GrandFather

hi actuvally i posted how to create module after careful reading i create one module but i have some errors can tel me how to modify

my 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 t +o 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" ); }

i created lik this

#!/usr/bin/perl package Mymodule; use warnings; use strict; use Exporter qw(import); use Carp; use XML::LibXML::Reader; our @EXPORT_OK(myFunction); sub MyFunction { my $fh = shift; my $reader = XML::LibXML::Reader->new( IO => $fh ) or die ("u +nable 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; my main function #!/usr/bin/perl use warnings; use strict; use Mypackage qw(myFunction); my $file; open( $file, 'formal.xml'); my %returnedHash = myFunction($file);

but i am getting errors like sysntax error at line 9 @export_ok in mypackage.pm and compilation error in main function line 4 (use mypackage qw(myfunction). i used right sysntax for export but i dnt know how to clear this.

Replies are listed 'Best First'.
Re: help me with errors in perl module.
by davido (Cardinal) on Oct 13, 2011 at 08:12 UTC

    Not the only problem, but one obvious problem is this:

    our @EXPORT_OK(myFunction); sub MyFunction {

    Actually, that's two problems. First, it should be our @EXPORT_OK = qw( myFunction ); # For example. Second, Perl identifiers are "case sensitive", so "myFunction" doesn't refer to the same entity as "MyFunction".

    Further down in your code, you also use Mypackage, but you created the package with the name "Mymodule".

    These sorts of details will haunt you throughout your programming career until you either pay attention to them or abandon the pursuit in favor of a more lenient endeavor.

    Those may not be the only problems. I just stopped reading the code after spotting so many problems with inconsistently named identifiers.


    Dave

      hi thanks dave i solved all my errors as you said it working. thanks.
Re: help me with errors in perl module.
by choroba (Cardinal) on Oct 13, 2011 at 08:15 UTC
    i used right sysntax for export
    If you mean this by "right sysntax" (sic!)
    our @EXPORT_OK(myFunction);
    you are mistaken. To assing to an array, use
    our @EXPORT_OK = qw(myFunction);
      thanks for your reply i modified and i got it now.
Re: help me with errors in perl module.
by perl_lover (Chaplain) on Oct 13, 2011 at 08:15 UTC
      thanks i got it.
Re: help me with errors in perl module.
by marscld (Beadle) on Oct 13, 2011 at 08:40 UTC
    The 1st line would not be necessary.

      It might be helpful to specify which first line.

      To fill in that blank, the first line that isn't necessary is the shbang line (the one that starts with #!/...) at the top of the module's source. It is necessary at the top of the main program's source, however.

      While we're at it, let's give a reason so that in the future it's easier to remember: The operating system needs to know what program to use to interpret the lines of code that follow. One could specify #!/bin/sh for a shell script, for example. But for a Perl script, we want to direct the operating system to our installed Perl interpreter.

      Since it is the main program that is invoked by the user as an executable command, that is where the shebang needs to be located. That's the only point where the operating system wouldn't know what to do, if the #!/... line weren't there. Once the main program is invoked and the Perl interpreter is fired up, perl handles calling and executing the modules. perl already knows where it lives, and doesn't need for the modules to specify a location. Luckily since the line is set up to look like a comment, it's just ignored when it appears at inappropriate places.


      Dave