tags:

views:

655

answers:

6

Hi,

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.

Thanks a bunch!

--Zain

+6  A: 

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

Daniel Plaisted
+4  A: 

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

Benj
+11  A: 

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.
martin clayton
This one is more directly comparable to C's #include from what I can tell, and is exactly what I needed.
Herms
+14  A: 

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.

daotoad
Check out Sub::Exporter it is all the rave!http://search.cpan.org/~rjbs/Sub-Exporter-0.982/lib/Sub/Exporter.pm
Evan Carroll
Additionally, if you're not installing the module in one of the default locations for perl modules, just add this before the "use Foo;" line: use lib ('C:/Module/Location');
Zain
Lets say I have libs from the default location and I want to use a module that is located in the local directory because it is really only used by the current app and the module was created to better organize the script. will the use lib line tell perl to only look at the location I specify for all module or does it add to the lib path that perl searches?
Jeremy Petzold
@Jeremy, check out http://stackoverflow.com/questions/2526804/how-is-perls-inc-constructed-aka-what-are-all-the-ways-of-affecting-where-per
daotoad
+2  A: 

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

aartist
+1  A: 

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

glenn jackman