Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with a homemade module

Status
Not open for further replies.

jerehart

Programmer
Jun 10, 1999
61
US
Hey,

I am trying to use a module which I have created but I can't seem to run it. I have done a perl -wc tester.pl and was told both the module and script syntax is fine. The script and the module are real simple:

tester.pl :
#!/usr/local/bin/perl

#this is a script to test the moveordel perl module

use mmroutines;
use Sample;

print "Before sub\n";

set_mm_system_vars();
APrint ();

print "After sub\n";

exit;

and Sample.pm:
#!/usr/local/bin/perl

sub APrint
{
print "This is a perl module\n";
}

the set_mm_system_vars() works no problem, but when trying to run tester.pl I receive the output:
Sample.pm did not return a true value at ./tester.pl line 6.
BEGIN failed--compilation aborted at ./tester.pl line 6.

Any ideas?

Miah
 
Every Perl module(.pm file) should have this skeleton:

Filename: abc.pm
-------------

package abc; ### first line in abc.pm

sub APrint
{
print "Something";
}

1; ### last line in abc.pm

--------------------
the important things are:
1. give package "abc" filename "abc.pm"
2. 1st line in abc.pm should be "package abc;"
3. last line in abc.pm should be "1;"

and that's it. Give that a try. HTH.

Hardy Merrill
Mission Critical Linux, Inc.
 
Hardy has given a good start with the basic rules. You can also check out .....
That is an Object Oriented 'toot' by Tom Christianson. I just sat down to write a new class myself, so I'll keep an eye on this thread along with Hardy.




keep the rudder amid ship and beware the odd typo
 
Thanks for that spot of help. I have something that is interesting though. I do not have the package or 1 in any of my modules, and they work. What I do have as the first line is:

print "\n\n";

sub whatever
{
#do something
}

Any ideas on this little curiosity?

Miah
 
I can't explain why that works(or why it doesn't give errors) - I've never really deviated from the prescribed method of creating perl modules. Doing "perldoc perl" lists these perl module topics:

perlmod Perl modules: how they work
perlmodlib Perl modules: how to write and use
perlmodinstall Perl modules: how to install from CPAN

You can then do "perldoc perlmod" to view that documentation, etc.

I also wanted to add to "goBoating"s comments - the skeleton I provided was about the simplest skeleton there is. It also assumes that package(or module) abc.pm is in the same directory as the script(s) that "use" it. There are other, more "correct"(and unfortunately more complex - or not as straight-forward) ways to create modules, such that you "make", "make test", and finally "make install" your module and the module ends up under the site_perl tree where it is accessible to any other perl scripts that run anywhere on that system.

The way for you to get your feet wet in perl modules is just the way you're doing it - start with simple modules that live in the same directory as the scripts that "use" them. When you're comfortable with that, or when you need to write modules that you want to be accessible from any perl script on that system, then move on to the actual "correct" way to write a perl module. I'm not yet a perl object oriented class person, but I'm going to start learning that soon - I'll be using "perldoc perltoot" as "goBoating" suggested. HTH.

Hardy Merrill
Mission Critical Linux, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top