4

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

1
  • Intermediate Perl covers this sort of stuff. Commented Feb 6, 2010 at 0:29

2 Answers 2

10

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

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

Sign up to request clarification or add additional context in comments.

Comments

5

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();

4 Comments

Can you provide a link to its documentation? Thanks.
@user: Which, Exporter? Try perldoc Exporter at the command line or terminal.
@user1923452345234234564563657758, perldoc.perl.org has the entire FM for your reading pleasure. perldoc.perl.org
@daotoad I think you mean user8675309.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.