How to create my own module??

I have a bit confuse on the procedure for making my own module. I know I have to use the command h2xs -x -n, but how do I do, I am totally lost !! I want to add this procedure to my module called MyModules
------------------
sub logistic{
  my ($xstart, $mv, $N);
  my @x;
  $x[0] = $xstart;
  for (my $i=0; $i<$N; $i++){
    $x[$i+1] = $mv * $x[$i] * (1-$x[$i]);
  }
  return @x;
}

THANKS !
LVL 2
clo1Asked:
Who is Participating?
 
freesourceConnect With a Mentor Commented:

In Windows you need to use escapes like this:

perl -e 'for (@INC) { print \"$_ \n\";}'

In ActiveState Perl you will probably get something like:
C:/Perl/lib
C:/Perl/site/lib

Since you don't need autoloader or XS, if you want to distribute your module do this.

h2xs -XA -n MYDIR::MYMODULES

Go into ./MYDIR and edit MYMODULES.pm (incidently, modules end with pm) the way you want it.

Do a "perl Makefile.PL"

I am not sure about Windows because the make program is probably different than the one I use in GNU/Linux, but then you would type "make dist" and you will get a tar archive ready for shipment!  

And here is the rest of the answer I gave you.. remember to file MYMODULES should actually be called MYMODULES.pm.

If this is the first time you ever made a module, ditch the h2xs thing and do something like what is below, and put the directory MYDIR in one of Perls @INC directories which you can find by doing a perl -e 'for (@INC) { printf "%d %s\n", $i++, $_ ;}' and the module MYMODULES in the MYDIR directory.  You can call the module in a program by doing a "use MYDIR::MYMODULES;" then just call the function as you normally would :


package MYDIR::MYMODULES;

use vars qw(@ISA @EXPORT);                                                      
use Exporter;                                                                  
@ISA = qw(Exporter);  
@EXPORT = qw(logistic);

sub logistic{
                      my ($xstart, $mv, $N);
                      my @x;
                      $x[0] = $xstart;
                      for (my $i=0; $i<$N; $i++){
                        $x[$i+1] = $mv * $x[$i] * (1-$x[$i]);
                      }
                      return @x;
                    }

1;

Let's say  perl -e 'for (@INC) { printf "%d %s\n", $i++, $_ ;}'  tells us this:
0 /usr/lib/perl5/5.005/i386-linux
1 /usr/lib/perl5/5.005
2 /usr/local/lib/site_perl/i386-linux
3 /usr/local/lib/site_perl
4 /usr/lib/perl5
5 .

Think of MYDIR::MYMODULES as ./MYDIR/MYMODULES.pm

Put it in one of these directories.  Note: there are ways to specify alternative  directories, but that is another story.

So you could put it in ...

/usr/local/lib/site_perl/i386-linux/MYDIR/MYMODULES.pm

MYMODULES.pm is the file which has the code in it.  It's that simple!  Put the code I gave you, and add other functions if you want to like  @EXPORT = qw(logistic anotherfunction); where anotherfunction is another function you wrote.

There are other things you can do with @EXPORT, but I am keeping to the basics so that you can understand what modules are about in an easy way .. ofcourse that is the whole point, Perl is suppose to make hard things easier :)
0
 
freesourceCommented:
If this is the first time you ever made a module, ditch the h2xs thing and do something like what is below, and put the directory MYDIR in one of Perls @INC directories which you can find by doing a perl -e 'for (@INC) { printf "%d %s\n", $i++, $_ ;}' and the module MYMODULES in the MYDIR directory.  You can call the module in a program by doing a "use MYDIR::MYMODULES;" then just call the function as you normally would :


package MYDIR::MYMODULES;

use vars qw(@ISA @EXPORT);                                                      
use Exporter;                                                                  
@ISA = qw(Exporter);  
@EXPORT = qw(logistic);

sub logistic{
                      my ($xstart, $mv, $N);
                      my @x;
                      $x[0] = $xstart;
                      for (my $i=0; $i<$N; $i++){
                        $x[$i+1] = $mv * $x[$i] * (1-$x[$i]);
                      }
                      return @x;
                    }

1;
0
 
clo1Author Commented:
Can you also tell me more detaily? Like what should I save the file as. And something like that !
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
freesourceCommented:
Let's say  perl -e 'for (@INC) { printf "%d %s\n", $i++, $_ ;}'  tells us this:
0 /usr/lib/perl5/5.005/i386-linux
1 /usr/lib/perl5/5.005
2 /usr/local/lib/site_perl/i386-linux
3 /usr/local/lib/site_perl
4 /usr/lib/perl5
5 .

Think of MYDIR::MYMODULES as ./MYDIR/MYMODULES.

Put it in one of these directories.  Note: there are ways to specify alternative directories, but that is another story.

So you could put it in ...

/usr/local/lib/site_perl/i386-linux/MYDIR/MYMODULES

MYMODULES is the file which has the code in it.  It's that simple!  Put the code I gave you, and add other functions if you want to like  @EXPORT = qw(logistic anotherfunction); where anotherfunction is another function you wrote.

There are other things you can do with @EXPORT, but I am keeping to the basics so that you can understand what modules are about in an easy way .. ofcourse that is the whole point, Perl is suppose to make hard things easier :)

0
 
clo1Author Commented:
Thanks for your detail instruction. One thing I forgot to tell is I am using ActivePerl build 522 instead of Unix one. Also, if I really want to use h2xs command(because I learn it from my professor), how do I do?

P.s. I have tried the perl -e....command that you gave me, and maybe it doesn't work in the ActivePerl. So I stopped trying.

Hey, post it as an answer pal, and I will give an A to you, cause I am appreciate about your effort. Not only this question, I mean all of the perl question I have asked. Thanks again !!
0
 
clo1Author Commented:
Adjusted points to 100
0
All Courses

From novice to tech pro — start learning today.