Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to create a seperate helper function in Perl?

Posted on 2012-12-20
4
Medium Priority
?
361 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 2000 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 27

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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

783 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