Home > Archive > PERL Beginners > April 2007 > Module writing
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| ../Test.pm
-------------------------------------------------
use strict;
package Test;
sub sayHello {
print "Hello";
}
1;
../Test.pl
-------------------------------------------------
use strict;
use Test;
Test::sayHello();
when i run Test.pl, it say:
Undefined subroutine &Test::sayHello called at Test.pl line 5...
any hints for this error?
thanks.
| |
| Purl Gurl 2007-04-22, 9:58 pm |
| howa wrote:
(snipped)
> ./Test.pm
> use strict;
> package Test;
> sub sayHello {
> print "Hello";
> }
>
> 1;
> when i run Test.pl, it say:
> Undefined subroutine &Test::sayHello called at Test.pl line 5...
remove package Test; from your test module.
change Test::sayHello(); to sayHello; in your script.
Save your test module in your current perl module directory
or set a valid path to your module.
You will find hundreds of tutorials on the web about writing
Perl modules.
http://www.google.com/search?hl=en&...G=Google+Search
Purl Gurl
| |
|
| > remove package Test; from your test module.
>
> change Test::sayHello(); to sayHello; in your script.
>
> Save your test module in your current perl module directory
> or set a valid path to your module.
>
firstly, thanks.
i follow your guideline, and now it said :
Undefined subroutine &main::sayHello called at...
in fact, why remove the package and Test::... ? i really want them
under the namespace Test and not putting the function in the main
codes...
thanks again.
| |
| Purl Gurl 2007-04-22, 9:58 pm |
| howa wrote:
[color=darkred]
> i follow your guideline, and now it said :
> Undefined subroutine &main::sayHello called at...
Then you did not follow my guidelines.
There are hundreds of tutorials on the web to teach
you about writing Perl modules,
http://www.google.com/search?hl=en&...G=Google+Search
Purl Gurl
| |
| Uri Guttman 2007-04-22, 9:58 pm |
| >>>>> "h" == howa <howachen@gmail.com> writes:
[color=darkred]
h> firstly, thanks.
you shouldn't do that. moronzilla is the main troll here and it never
directly answers your question.
h> i follow your guideline, and now it said :
h> Undefined subroutine &main::sayHello called at...
h> in fact, why remove the package and Test::... ? i really want them
h> under the namespace Test and not putting the function in the main
h> codes...
telling you to remove package calls and the package name is typical crap
from moronzilla since you obviously want to use package spaces.
for a correct answer and fix, first you should make sure your module
Test.pm is being loaded. put this line in Test.pm (not in the sub):
print "LOADED\n" ;
and run it again. when i did, LOADED was not printed. this means perl is
loading some other module named Test. this is easy to fix by renaming
your module to Test2.pm (or some other unique name). then change the use
line to use that name. you don't need to change the package names for
this to work. when you run it you should see LOADED and Hello. another
minor fix is to put a newline after hello in the print.
a module name like Test is poor as it is very generic and perl may
(obviously does) have such a module and it finds it first. another fix
is to make the current dir searched before the perl lib dirs. this can
be done with this:
perl -I. Test.pl
and it fixes it for me as well. the Test.pl script can also put this
line:
use lib '.' ;
before it uses Test.pm and it also works.
so there are several ways to diagnose and fix this. but first you must
ignore moronzilla as you won't learn anything from it.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
| |
| Paul Lalli 2007-04-23, 7:58 am |
| On Apr 22, 7:37 am, howa <howac...@gmail.com> wrote:
> ./Test.pm
> -------------------------------------------------
> use strict;
>
> package Test;
>
> sub sayHello {
>
> print "Hello";
>
> }
>
> 1;
>
> ./Test.pl
> -------------------------------------------------
> use strict;
>
> use Test;
>
> Test::sayHello();
>
> when i run Test.pl, it say:
>
> Undefined subroutine &Test::sayHello called at Test.pl line 5...
>
> any hints for this error?
You did everything fine, you just chose a poor name. "Test" is a
built-in module in Perl, so when you said "use Test;", Perl loaded its
own Test.pm module, not yours.
Just choose a different name for your package. MyTest.pm or similar
will be fine.
Paul Lalli
|
|
|
|
|