How to include functions from another file in a Perl script?

This seems like a very simple question, but somehow my Google Fu couldn't get me.

What is the syntax for including functions from other files in Perl? I am looking for something like C #include "blah.h"

I saw an option for using Perl modules, but it looks like it will require a slight rewrite of my current code.

+58
include perl
Nov 10 '09 at 23:34
source share
8 answers

Use the module. Check out perldoc perlmod and Exporter .

In the file Foo.pm

 package Foo; use strict; use warnings; use Exporter; our @ISA= qw( Exporter ); # these CAN be exported. our @EXPORT_OK = qw( export_me export_me_too ); # these are exported by default. our @EXPORT = qw( export_me ); sub export_me { # stuff } sub export_me_too { # stuff } 1; 

In your main program:

 use strict; use warnings; use Foo; # import default list of items. export_me( 1 ); 

Or to get both functions:

 use strict; use warnings; use Foo qw( export_me export_me_too ); # import listed items export_me( 1 ); export_me_too( 1 ); 

You can also import package variables, but the practice is very discouraged.

+59
Nov 11 '09 at 0:12
source share

Perl require will do the job. You will need to make sure that any required files return the truth by adding

 1; 

at the end of the file.

Here's a tiny example:

 $ cat m1.pl use strict; sub x { warn "aard"; } 1; $ cat m2.pl use strict; require "m1.pl"; x(); $ perl m2.pl aard at m1.pl line 2. 

But go to the modules as soon as you can .

EDIT

Several advantages of porting code from scripts to modules:

  • Without packages, everything occupies the same namespace, so you may run into a situation where two functions from separate files want to have the same name.
  • The package allows you to expose some functions, but hide others. Without packages, all features are visible.
  • Files included in require are only loaded at runtime, while packages loaded with use undergo earlier compile-time checks.
+39
Nov 10 '09 at 23:39
source share

I believe you are looking for require or use .

+7
Nov 10 '09 at 23:37
source share

In addition, do 'file.pl'; will work, but modules are the best solution.

+5
Nov 12 '09 at 11:49
source share

I know that the question specifically speaks of β€œfunctions”, but I get this post high in search when I search for β€œperl include”, and often times (for example, now) I want to include variables (in a simple way, without thinking about modules) . And so I hope that my example will be published here (see Also: Perl require and variables ; in short: use require and make sure that both the "includeser" and "includesee" files declare the variable as our ):

 $ perl --version This is perl, v5.10.1 (*) built for i686-linux-gnu-thread-multi ... $ cat inc.pl use warnings; use strict; our $xxx = "Testing"; 1; $ cat testA.pl use warnings; use strict; require "inc.pl"; our $xxx; print "1-$xxx-\n"; print "Done\n"; $ perl testA.pl 1-Testing- Done $ cat testB.pl use warnings; use strict; our $xxx; print "1-$xxx-\n"; $xxx="Z"; print "2-$xxx-\n"; require "inc.pl"; print "3-$xxx-\n"; print "Done\n"; $ perl testB.pl Use of uninitialized value $xxx in concatenation (.) or string at testB.pl line 5. 1-- 2-Z- 3-Testing- Done 
+4
Jan 20 '13 at 11:33
source share

You really should learn perl modules, however, for a quick hack you can always run "perl -P", which runs your perl script through the C preprocessor. This means you can make #include and friends too ....

Just a quick hack, be careful; -)

+3
Nov 10 '09 at 23:37
source share

What you are looking for is "require file.pl", but what you should look for is "use the module."

+3
Nov 11 '09 at 1:18
source share

The above answers to all are ignored by the client part: how to import a module.

See the accepted answer here: How to use the Perl module from a relative location?

Without the trick in this answer, you will have many problems trying to get the module path when you use $mymodule;

+1
Mar 06 '14 at 15:43
source share



All Articles