Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a CGI script within a CGI script?

Status
Not open for further replies.

KingofSnake

Programmer
Jul 25, 2000
73
US
Is it possible to call a CGI script within a CGI script, much like you call a subroutine?
KingOfSnake - The only sports drink with ice crystals
(and marshmellos!)
 
Sure, just:

[tt]
require "script.cgi";
[/tt]

But make sure that at the end of script.cgi, there is a 1; to make sure the script executes.

Hope this helps.

-Vic vic cherubini
krs-one@cnunited.com
 
So I'll just have the last line in 'script.cgi' as "1"? Seems strange, but if that's what Perl wants I'll do it.


KingOfSnake - The only sports drink with ice crystals
(and marshmellos!)
 
You can also say

Code:
do "script.cgi";

This is better in some cases. -gerrygerry
misanthropist01@hotmail.com
 
Everybody keeps saying that.....What does that mean?
KingOfSnake - The only sports drink with ice crystals
(and marshmellos!)
 
There's More Than One Way To Do It

...and yes, you will see that a lot with Perl :) -gerrygerry
misanthropist01@hotmail.com
 
uh-oh...., kingofsnake.... you've joined the ranks of the Perl geeks.... Before long you'll be quoting Larry or Tom or one of the other Perl Gods.

The 'require' trick is quite simple. You just use the require statement to pull the second piece of code into the first. They then behave as one.

First piece of code, named code1.pl
Code:
#!/usr/local/bin/perl
require code2.pl;
&something_in_code2;

The second piece of code, named code2.pl.
Code:
#!/usr/local/bin/perl
# the previous line may or may not be needed depending on wether you
# might run code2.pl without calling it from code1.pl or some other.
sub something_in_code2
{
print "Ran a sub in the second piece of code.\n";
}
1;

The 'require' line in code1.pl needs to return a 'true' status. That is what the '1' at the end of code2.pl does. It simply returns a '1' so the require sees a successful status.

HTH


keep the rudder amid ship and beware the odd typo
 
Or,

[tt]
@a = (Tifsft, Mpsf, Tibo, Oof, Wbz,
Tp, Dp, Iu); $b = "Io Pfsm...";$b =~ y/b-z/a-z/;
for ($i=0; $i<=$#a; $i++) { $a[$i] =~ y/b-z/a-z/;
$b .= &quot; $a[$i]&quot;; }print $b;
[/tt]

:):):):)

Just a little piece of obsfucated code I wanted to write to say TMTONWTDI.

-Vic vic cherubini
krs-one@cnunited.com
 
There are some differences between &quot;require&quot; and &quot;do&quot; to keep in mind. The major one is that when you use &quot;do&quot; the file is reparsed every time the &quot;do&quot; statement is executed, whereas a &quot;required&quot; file will NOT be pulled in twice under the same name. Also &quot;do&quot; does not necessarily force you to return true, whereas &quot;require&quot; does. Both commands will search the current include path, keep track of the file name for error messages, etc. Other than that, &quot;require&quot; is pretty much a variant of &quot;do&quot;, with some additional semantics to prevent reparsing. As a matter of convention, &quot;require&quot; is usually used to pull in definitions or variables, subroutines, etc. while &quot;do&quot; is usually used to execute an &quot;external&quot; perl program. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
A perl geek? Me. That is bad.... :D

KingOfSnake - The only sports drink with ice crystals
(and marshmellos!)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top