Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reuse some functions in a Perl script?

Tags:

perl

I am facing a problem that require the reuse some of the functions within another Perl script. I am writing some test scripts. The test scripts are basically build on each other.

Say Script 1 does:

Some code to prepare the test. A. B. C. Some code to determine the success.

Then Script 2 does:

Some code to prepare the test. A. B. C. D. E. Some code to determine the success.

How can I reuse A.B.C of script 1 in script 2?

Calling script 1 from script 2 will not work because of the code to determine the success of the script. What is the best way to do this?

Thanks

like image 426
user195678 Avatar asked Dec 08 '22 04:12

user195678


2 Answers

Put the functions in a module and include that from both files.

See http://perldoc.perl.org/perlmod.html for more info.

like image 165
Corey Avatar answered Dec 09 '22 16:12

Corey


Foo/Common.pm:

package Foo::Common;
use strict;
use warnings;
use parent 'Exporter';
our @EXPORT_OKAY = qw(frob borf);

sub frob {}
sub borf {}

1;

In some script or module, give or take a use lib to get Foo/Common.pm in @INC:

use Foo::Common qw(frob borf);
frob();
like image 40
Anonymous Avatar answered Dec 09 '22 16:12

Anonymous