# Perl: How do I "Require" or "Include" another perl script file?



## Jasoco (Feb 13, 2002)

Say I have this one Perl file (page.pl) and I have a separate Perl file (subs.pl) that I want to call Subroutines in.

I'd keep the Subs in the subs.pl file and from my other PL file I would call the subroutine. But how do I "Require" or "Include" the "subs.pl" file?

This is the only thing holding me from a total site redesign.


----------



## martinatkinson (Feb 16, 2002)

Hello!

I am just learning CGI so I do not know if this is right but I think all you have to do is put the following line of code into your file underneath your #!/usr/bin/perl line:


```
require "subs.pl";
```

Let me know if this works.

Have a great day!

Albert


----------



## Jasoco (Feb 18, 2002)

Nope. Doesn't work.



> *CGI Error
> 
> The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:
> 
> Can't locate settings.pl in @INC (@INC contains: D:/activeperl/lib D:/activeperl/site/lib .) at D:\Inetpub\jasoco\cgi-bin\test.pl line 4.*



Seems the test files I created aren't written right I guess?

test.pl

```
use CGI;
#!/usr/bin/perl

require "settings.pl";

print "Content-type: text/html\n\n";

&TestSub;
```

settings.pl

```
use CGI;
#!/usr/bin/perl

Sub TestSub {
print "Hello!";
}
```


----------



## martinatkinson (Feb 18, 2002)

Hello!

Is the file "settings.pl" in the same directory as your main CGI script?  It will need to be.  If this does not work try using the absolute path to "settings.pl"

Let me know what happens!

Albert


----------



## Jasoco (Feb 18, 2002)

The Settings file is in the same directory as the Test file. The error page seems to imply the program is looking in its own directory for the files so I tired an absolute path (D:/Inetpub/jasoco/cgi-bin/settings.pl) Which seems to get passed THAT, but now I have a whole new error.



> CGI Error
> 
> The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:
> 
> syntax error at D:/Inetpub/jasoco/cgi-bin/settings.pl line 6, near "}"



I seem to have it Requiring right,  but now I have a problem with the Required files code.

Am I doing the Sub wrong? Or what? This whole Require part is the ONLY thing keeping me from advancing my site further and I've been racking my brain trying to figure it out!


----------



## martinatkinson (Feb 18, 2002)

Hello!

Hmmm, looks like my code only works if the contents of your settings.pl file are defining variables, for example, this is a clip out of one of my scripts:

# path to template file
$template_file="template.html";


And then in your main file you could do something like:

require "config.pl";

#program start

$top_page=top_page($template_file);
$bottom_page=bottom_page($template_file);

Other then that I do not know, I am just starting to learn Perl so I guess I was not much help.

Have a great day!

Albert


----------



## Jasoco (Feb 18, 2002)

Don't feel bad. You're the third person to try and help me with Requiring. I can't seem to find info anywhere. Noone seems to know how to do it the way I need it done. Hmmm


Well, I guess I'll look on Perl.com and CPAN again later.

If they don't help, can anyone recommend a Perl book?

I wonder if my server supports PHP...? Anyone have a way I can test? I don't own the server. The only way I know I can do Perl is that I put a file up and ran it. When it worked, I knew. But I need a sample PHP file I can upload and try. My host didn't tell me anything about what I can do and what I can't.


----------



## Jadey (Feb 18, 2002)

I've never had to work with CGI on NT, so don't know if this will help. Try using the http path to the script for the require, like:

require "http://www.domain.ca/cgi-bin/settings.pl";


----------



## snydersoft (Jun 18, 2006)

Obviously this thread is OLD and my assistance probably isn't needed. But I surfed to it while looking for something else and who knows...maybe someone else will so I am going to add my thoughts on the matter in case it can help them out.

As for the headers problem. While the text/html header was in the test file, you will notice that it wasn't in the settings file (the included file).  The error received would suggest that the proper headings aren't being sent by the settings file so I would add the text/html header line to the included file.

When including a file, the included file still processes whatever subroutine you call and then sends the result to the original file which places the result into it's own results in that place.  Therefore since the settings file is trying to print text/html to the browser, the script isn't sending the proper headers to the browser to tell it what to expect.  The settings file then sends the error to the main script.  So the originating script doesn't have an error in it.

Now, once this is taken care of (if that would actually fix the problem) you should also add the following..I am going to add it on its own line so you can see exactly what it is:

1;

That should be the last line of ANY file that you include in any script.  When you add a require the script is going to test the included file for a value. Adding that line will make the require a "true" statement and therefore allow it to run.

Now the error that was received may suggest that that wasn't a problem. But it's safe practice to do so. Especially if a script is being programmed for use by many people on many systems.  You want everything to be as portable as possible.

Hopefully this will help someone out someday.


----------



## SeniorSE (Mar 11, 2008)

I'm kind of new to perl, but I see an error in your code above and I know how to accomplish what you want.

First, the hash-bang line needs to be the first line in the perl script (before the use command).

Second, when using "use" or "require", it appears that the system does not want .pl extensions, but rather .pm extensions.  So, if you have code that looks like this:

prog.pl

```
#!/usr/bin/perl -w

use newsub;

&newsub;

exit;
```

newsub.pm

```
#!/usr/bin/perl

sub newsub {

print "hello\n";

}

return 1;
exit;
```

And, make sure that the newsub.pm file is in the same directory as the perl script you are running it with.  optionally, you can figure out how to add it to your perllocal.pod config file, so it will be global to your perl installation.  (I don't know how to do that, though.  )

Good luck,
Mark


----------



## ravipatel4 (Jun 30, 2008)

The problem of the header error is due to the wrong positions of lines.
You need to add the header line first.
Means put the line
print "Content-type: text/html\n\n";
before the "require" statement.
It will definitely solve your problem.
contact me if you have any other problem.
You can contact me at www.liveperson.com/ravi-patel

Good luck.
Bye

Ravi Patel


----------



## skholastikos (Mar 7, 2009)

I was also having the same problem where INC was not able to find the file to be included, even though it was in the same directory as the main.pl file. The problem was not in my use of the 'require', but in the way I was running the program. 

In terminal (on mac) I was running the program from the root directory by using the command:


```
/Users/jsgoyette/Desktop/main.pl
```

However in doing so, it seems the file to be included was being searched for in the root directory. The problem was thus simply corrected by moving to the Desktop, where I had program.pl, by:


```
cd desktop
```

and then by running the program with:


```
./main.pl
```

I'm not sure what all of this means, but for future newbies like me, I thought it was worth mentioning.


----------

