Solved

How to create a seperate helper function in Perl?

Posted on 2012-12-20
4
301 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

828 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