Operating System - HP-UX
1752781 Members
6695 Online
108789 Solutions
New Discussion юеВ

Need simple perl example of creating, executing and returning information from a perl module

 
SOLVED
Go to solution
Jack C. Mahaffey
Super Advisor

Need simple perl example of creating, executing and returning information from a perl module

I would like to do the following:
1 - Create a perl module that will provide a list of entries from a file using perl.
2 - Module will ask user to select an entry from the file.
3 - Data from the file will be returned from the module to the calling program.

I'm kind looking to see what I would need to do to code a module and be able to access the module expecting to make further decisions depending upon the results results.

jack...
6 REPLIES 6
Ralph Grothe
Honored Contributor

Re: Need simple perl example of creating, executing and returning information from a perl module

Hi Jack,

at the shell type

perldoc perlmod
perldoc perlmodlib

for howto open files, and read/write data

perldoc perlopentut

and if your module shall be OO

perldoc perlboot
perldoc perltoot
perldoc perltootc

These will give you enough to get started.

You have to decide whether your module will follow procedural or object oriented design.

For the first it's easiest to let your Module inherit from the Exporter module.
Don't be afraid, there's not much more OO involved than assigning the @ISA array:

require Exporter;
@ISA = qw(Exporter);

For the latter (viz. OO design) you will have to write a constructor (aka class method) that uses the bless() function.
With it you "bless" any data structure (preferably a hashref) to your package's namespace.
Then you have to write accessor methods (ordinary subs that take an object ref as 1st argument) to deal with your "encapsulated" data.
The rest is ordinary Perl idioms (OO purists will cry out ;-)

It is important that your module returns any true value.
The Perl idiom for this requirement is a
1;
as last statement.
Madness, thy name is system administration
Jack C. Mahaffey
Super Advisor

Re: Need simple perl example of creating, executing and returning information from a perl module

A little over my head. I was hoping to get a cut-paste example that I could figure out fairly quickly. I've taken the perl programming class and now ran into a situation where it would be nice to use a home grown module. I'm kinda short on time and resources to get too involved reading the man pages.

Thanks for the pointers... When I get back to it, I'll follow your suggestions.


Thanks for the help.


Gregory Fruth
Esteemed Contributor

Re: Need simple perl example of creating, executing and returning information from a perl module

The problem is a little vague: what type of file are you
trying to read? A tab-delimited flat text file? A binary file? A
database?

The second part is a little easier (assuming you want
to get the user's reponse from stdin (i.e. the keyboard)):

print("what entry do you want?\n");
$entry = <>;

If you want to get fancy and provide command line
editing at the prompt, check out the Perl module
Term::Readline.

The last part, like the first part, depends on the
type of file.

As for "module", if you mean a real Perl module
with the "::" business and all that OO-ish stuff,
definitely read "perldoc permod" or "man perlmod".
It has an example of setting up a module.

If you have just learned Perl it might be better to
first create a set of plain Perl subroutines and worry
about packaging them into a module later.

HTH
Jack C. Mahaffey
Super Advisor

Re: Need simple perl example of creating, executing and returning information from a perl module

My situation is as follows:

I have a piece of code that acts as a menu for selecting entries from a file. This same code is used by multiple scripts. I wanted to turn the menuing code into a module so that I could call the module and then use the result returned from the module. I would pass the program name of the calling program to the menu module.

jack...
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Need simple perl example of creating, executing and returning information from a perl module

just put '1;' at the end, *DO*NOT* use the word 'package' and move the code to be shared as 'your_module.pm' to somewhere inside the INC path 'perl -le "print@INC"'

thean use

'use my_module;'

inside your own script, and the *complete* file is included into your current script (package main)

HTH
Enjoy, Have FUN! H.Merijn
Jack C. Mahaffey
Super Advisor

Re: Need simple perl example of creating, executing and returning information from a perl module

It will be a week or more before I can get back to the scripts. Will let you know how it goes.


Thanks for the help.


jack...