Solved

How to create a seperate helper function in Perl?

Posted on 2012-12-20
4
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans

734 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