How do I include functions from another file in my

2020-01-25 04:53发布

问题:

This seems like a really simple question but somehow my Google-Fu failed me.

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

I saw the option for using Perl modules, but that seems like it'll require a not-insignificant rewrite of my current code.

回答1:

Use a module. Check out perldoc perlmod and Exporter.

In 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 strongly discouraged.



回答2:

Perl require will do the job. You will need to ensure that any 'require'd files return truth by adding

1;

at the end of the file.

Here's a tiny sample:

$ 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 migrate to modules as soon as you can.

EDIT

A few benefits of migrating code from scripts to modules:

  • Without packages, everything occupies a single namespace, so you may hit a situation where two functions from separate files want the same name.
  • A package allows you to expose some functions, but hide others. With no packages, all functions are visible.
  • Files included with require are only loaded at run time, whereas packages loaded with use are subject to earlier compile-time checks.


回答3:

I believe you are looking for the require or use keywords.



回答4:

Also, do 'file.pl'; will work, but modules are the better solution.



回答5:

I know the question specifically says "functions", but I get this post high up in search when I look for "perl include", and often times (like now) I want to include variables (in a simple way, without having to think about modules). And so I hope it's OK to post my example here (see also: Perl require and variables; in brief: use require, and make sure both "includer" and "includee" 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


回答6:

You really should look into perl modules however, for a quick hack you could always run "perl -P" which runs your perl script through the C pre-processor. That means you can do #include and friends....

Only a quick hack though, beware ;-)



回答7:

What are you looking for is 'require file.pl', but what you should be looking at is 'use module'.



回答8:

The above answers all ignored the client part: How to import the module.

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

Without the trick in this answer, you'll have plenty of trouble trying to get the module path right when you use $mymodule;



标签: perl include