Solved

How to create a seperate helper function in Perl?

Posted on 2012-12-20
4
284 Views
Last Modified: 2012-12-20
Hi,
I have a few Perl packages. In these packages, there is a piece of code that I repeat every time. Therefore, I would like to create a separate function (a separate function in a helperFunctions directory) and when I need that piece of code, I will call this function with certain inputs and return the output to the caller.

Example:
package xxx::yyy;
use strict;
use warnings;

sub yyy{

some code-1

some repetitve code-1
some repetitve code-2
some repetitve code-3
some repetitve code-4
some repetitve code-5

some code-2

}
1;

Open in new window


I want this code to be like this:
package xxx::yyy;
use strict;
use warnings;

sub yyy{

some code-1

call helperFunction lineNumbers.pm here and return the output

some code-2

}
1;

Open in new window


How can I do it?

Can you send me a basic hello world example?

the directory structure is like this:

/dir1/yyy.pm

Open in new window


The helperFunctions dir will be like this:

/dir1/helperFunctions/lineNumbers.pm

Open in new window

0
Comment
Question by:Tolgar
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
whosbetterthanme earned 500 total points
ID: 38710629
you can try this:

#Add this to your package:
require '/dir1/helperFunctions/lineNumbers.pm';

#Call the subroutine as normal:
&helperFunction;

#Then in lineNumbers.pm add
sub helperFunction{
#Code here
}

1; #don't forget to have this!!!

Open in new window

0
 

Author Comment

by:Tolgar
ID: 38710646
@whosbetterthanme: Is "require" the only way to do this?
0
 
LVL 7

Expert Comment

by:whosbetterthanme
ID: 38710724
I believe you can use "use" also:
http://perldoc.perl.org/functions/use.html
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 38711091
Using "use" is more standard.  Generally, you'll write a module something like:
package MyFunctions;
require Exporter;
use base qw(Exporter);
@EXPORT = qw(helperFunction1);
sub helperFunction1 {
...
}
1;

Open in new window

And then your program will have something like:
use lib 'some-dir'; # only needed if MyFunctions is not in somewhere pointed to by @INC
use MyFunctions;
helperFunction1();

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now