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

hi,

i'm having some trouble with writing a module. i'm working under win32 and i would like to create a module. actually, just pass some subroutines into a module for organizing purposes. example:

"Test.pl" #use lib "./Nzd/"; ->not sure if this should be here #push @INC, "./Nzd/"; ->not sure if this should be here use strict; use warnings; use Nzd::NZD; #Nzd is a directory containing NZD.pm my ($x,$y)= (8,36); my $t=NZD($x,$y); my $c=$x/$t; my $g=$y/$t; print "\nc= $c, g= $g"; ------------------------------------ "NZD.pm" package Nzd::NZD; use strict; sub NZD { my ($x, $b) = @_; if ($x > $b){ ($x,$b) = ($b,$x); } while ($x) { ($x, $b) = ($b % $x, $x); } return $b; } 1;
as i tried to show i have 2 scripts *.pl and *.pm and from Test.pl i would like to call a sub NZD which is situated in NZD.pm, that is in a ./Nzd directory. and obviously i don't know how to do it. message i'm getting is that i didn't define the NZD sub

"Undefined subroutine &main::NZD called at Test.pl line 10."

where is the problem ?

i'm doing wrong...

thanks for all your help

Replies are listed 'Best First'.
Re: how to write a module
by toolic (Bishop) on Jun 13, 2008 at 17:29 UTC
      i tried exactly the same example as the tutorial states and i'm receiving the same message:

      use strict; # you may need to set @INC here (see below) my @list = qw (J u s t ~ A n o t h e r ~ P e r l ~ H a c k e r !); #case 1 use MyModule; print func1(@list),"\n"; print func2(@list),"\n"; # case 2 # use MyModule qw(&func1); # print func1(@list),"\n"; # print MyModule::func2(@list),"\n"; # case 3 # use MyModule qw(:DEFAULT); # print func1(@list),"\n"; # print func2(@list),"\n"; # case 4 # use MyModule qw(:Both); # print func1(@list),"\n"; # print func2(@list),"\n"; ------------------------------------- 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;
      could it be because i'm on win32, or i'm just too tiered to see the obvious?
        Case 1 doesn't work because @EXPORT is empty. The symbols in @EXPORT_OK are available for importing but they are not automatically imported.
        The tutorial states that Case 1 will generate errors:
        Case 1: Because our module exports nothing by default we get errors...

        When I run Case 1, I get this error, as expected:

        Undefined subroutine &main::func1 called at ...

        Case 3 will also generate an error; Cases 2 and 4 should work properly.

Re: how to write a module
by pc88mxer (Vicar) on Jun 13, 2008 at 17:24 UTC
Re: how to write a module
by sasdrtx (Friar) on Jun 13, 2008 at 17:26 UTC
    You need to either export 'NZD' from NZD.pm, or Test.pl must use its fully-qualified name. See perlmod for more information.

    btw, NZD.pm is a module, not a script.

    Update: btw2: You have 3 different NZDs, which could be confusing. Make them different if you can.


    sas
Re: how to write a module
by Zen (Deacon) on Jun 13, 2008 at 18:32 UTC
    ----------------
    "Test.pl"

    use lib './Nzd';
    use NZD;
    ----------------
    "NZD.pm"

    package NZD;