Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Packages With Perl

I seem to be having a lot of trouble with making my first, simple Package (actually it is my first package period). I am doing everything I should be doing (I think) and it still isn't working. Here is the Package (I guess you can call it a Module):

package MyModule;

use strict;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);

$VERSION     = 1.00;
@ISA         = qw(Exporter);
@EXPORT      = ();
@EXPORT_OK   = qw(func1 func2);
%EXPORT_TAGS = ( DEFAULT => [qw(&func1)],
             Both    => [qw(&func1 &func2)]);

sub func1  { return reverse @_  }
sub func2  { return map{ uc }@_ }

1;

I saved this module as MyModule (yes, it was saved as a .pm file) in Perl/site/lib (this is where all of my modules that are not built-in are stored). Then I tried using this module inn a Perl script:

use strict;
use warnings;

my @list = qw (J u s t ~ A n o t h e r ~ P e r l ~ H a c k e r !);

use Mine::MyModule qw(&func1 &func2);
print func1(@list),"\n";
print func2(@list),"\n";

I save this as my.pl. Then I run my.pl and get this error:

Undefined subroutine &main::func1 called at C:\myperl\examplefolder\my.pl line 7.

Can someone please explain why this happens? Thanks in advance!

Note:Yes my examples were from Perl Monks. See the Perl Monks "Simple Module Tutorial". Thank You tachyon!

like image 962
Dynamic Avatar asked Aug 24 '11 14:08

Dynamic


People also ask

How do you create packages and modules in Perl?

BEGIN and END BlocksEvery BEGIN block is executed after the perl script is loaded and compiled but before any other statement is executed. Every END block is executed just before the perl interpreter exits. The BEGIN and END blocks are particularly useful when creating Perl modules.

What is package in Perl script?

A Perl package is a collection of code which resides in its own namespace. Perl module is a package defined in a file having the same name as that of the package and having extension . pm. Two different modules may contain a variable or a function of the same name.

How many Perl modules are there?

Perl modules are a set of related functions in a library file. They are specifically designed to be reusable by other modules or programs. There are 108,000 modules ready for you to use on the Comprehensive Perl Archive Network.


1 Answers

Your package name and your use name don't match. If you have your module in a folder called Mine then you need to name your package accordingly:

package Mine::MyModule

If you don't have it in that folder then you need to remove that from your use call

use MyModule

like image 123
Cfreak Avatar answered Oct 15 '22 12:10

Cfreak