Home > Perl > How do you include another file like #include in C code?

How do you include another file like #include in C code?

There are three ways to include another file in Perl. Each have their distinction edge, but all gets the job done.

Method #1: Use the command ‘use’.

Sample code:

# Include the file "sample_include.pm"
use sample_include;

Comment:

Note that the include file has to be Perl module (*.pm). For further reading on how to use command ‘use’, refer to the perldocs either through the command line or website:

perldoc -f use

What the above code does is load sample_include.pm as part of the file. Generally, this will contain some sort of package and subroutines, which can be called explicitly such as <package name>::<subroutine>. See example below:

# sample_include.pm
package sample_include;
sub test {
print "This is a subroutine";
}
1; # Needed here to return true when loading PM files.

# Sample program
use sample_include;
sample_include::test()  # calls subroutine test.

There are methods to allow nonexplicit package (also known as barewords such as test()). See Exporter and Classes for more info.

Method #2: Use the command ‘require’.

Sample code:

# Include the file "sample_include.pm"
require sample_include;

Comment:

Like the first method, this also needs to be a Perl module. For further reading on how to use this command, type the following or go here:

perldoc -f require

What’s the difference between require and use?

Technically, the use command uses require when loading the module. Use does more work, trying to import subroutines or variables into the main file. The below two section are equivalent:

# Load in sample_file.pm
use sample_file;

# Load in sample_file.pm
BEGIN {
require sample_file;
sample_file->import( LIST );
}

Method #3: Use the command ‘do’.

Sample code:

# Include the file "sample_include.pm"
do "sample_include.pm"

Comment:

Unlike the other two commands, ‘do’ does not require to be a Perl Module. However, it should be a Perl file, since it will execute the contents of the file. See here for more details.

One interesting note, the do mechanism can only be called once. The other mechanisms work if you include them multiple times in a .pm file, but the ‘do’ in a Perl module can only be called once (whether it’s intentional or not). Example:

#sample_include.pm
do "another_file.pl";
1

#another_include.pm
package another_include;
use sample_include;
&func();
1

# main file
use sample_include;
use another_include;
&func();

# another_file.pl
sub func {
print "Who will run this function?";
}

The main file will execute sample_include.pm first (before another_include). Do will execute and associate func to be with main::func(). However, when another_include.pm tries to run sample_include.pm, it ends up unable to work (for reasons unknown). As a result, when sample_include tries to run &func(), it will return an error.

The proper method is to make sure that ‘do’ command will only run once anywhere when including the files. Otherwise, some functions will not work as intended.

>perl main.pl
Undefined subrounte &another_include::func called at another_include.pm line 4.

For more reference (and a better tutorial), here’s another article that explains more in detail about includes: Including files.

Advertisements
Categories: Perl
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: