×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Problems with a homemade module

Problems with a homemade module

Problems with a homemade module

(OP)
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  

RE: Problems with a homemade module

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.
http://www.missioncriticallinux.com

RE: Problems with a homemade module

Hardy has given a good start with the basic rules.  You can also check out ..... http://www.perl.com/CPAN-local/doc/manual/html/pod/perltoot.html

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

RE: Problems with a homemade module

(OP)
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

RE: Problems with a homemade module

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.
http://www.missioncriticallinux.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close