Link to home
Start Free TrialLog in
Avatar of clo1
clo1

asked on

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 !
Avatar of freesource
freesource

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;
Avatar of clo1

ASKER

Can you also tell me more detaily? Like what should I save the file as. And something like that !
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 :)

Avatar of clo1

ASKER

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 !!
ASKER CERTIFIED SOLUTION
Avatar of freesource
freesource

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of clo1

ASKER

Adjusted points to 100