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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to call variables and subroutines that are... 2

Status
Not open for further replies.

Eloff

Programmer
Aug 29, 2001
78
0
0
CA
defined in a nother cgi script? Suppose I have data.cgi, where $number is defined and &number is defined. I want to call these from call.cgi. How would I do this? Thanks.
 
Sure you can. All you need to do is use the require function.

Try this:

require "data.cgi";

#now you can use all the variables from data.cgi ----------------------------------------
There is no Knowledge, That is not power.

Yes, i'm a MySQL Newbie.
-Aaron
----------------------------------------
 
Hey for once something so simple I couldn't possibly mess it up. Thnx.
 
If you're depending your whole site on the data.cgi, then you can add more security stuff to make sure it's running smoothly.

require "data.cgi" or &error("Data File is missing!","Error");

sub error {
my($error, $title) = @_;
print &quot;Error: $error<BR>&quot;;
exit;
}

You don't have to but hey, take advantage of it:) ----------------------------------------
There is no Knowledge, That is not power.

Yes, i'm a MySQL Newbie.
Age: 16
E-mail: projectnet01@cs.com
Company:(not done yet) :)
-Aaron
----------------------------------------
 
Oh by the way, anybody know why i'm getting those &quot;stars&quot; automatically without anyone clicking on it? That happened last post, too. Don't worry, it's not counted. :) ----------------------------------------
There is no Knowledge, That is not power.

Yes, i'm a MySQL Newbie.
Age: 16
E-mail: projectnet01@cs.com
Company:(not done yet) :)
-Aaron
----------------------------------------
 
hehe, that star thing happened last time to me. Don't worry, it'l go away:)
 
Hmm the trouble with require is it does the other cgi script over again. I just want to be able to call variables that were defined in the other cgi script, not to print it out all over again.
 
I forgot to mention one thing. Open up the data.cgi and add this line at the very end of the script:

#add this to the end of script..
1;
#end of code


Do not delete that 1 and try again.
----------------------------------------
There is no Knowledge, That is not power.

Yes, i'm a MySQL Newbie.
Age: 16
E-mail: projectnet01@cs.com
Company:(not done yet) :)
-Aaron
----------------------------------------
 
No good, it still just adds the origional cgi script to the one with the require code in it. check for yourself. I don't want it to run yahtzee.cgi again, but I do want roll.cgi to be able to use variables and subroutines from yahtzee.cgi.
 
I found the problem. You need to add an exit function after the code Here's an example:

if ($FORM{'email'} eq &quot;&quot;) {
print &quot;error&quot;;
exit;
}
Yes, it's a bad example but I don't know how your subroutine looks like. If you could show me your code, then i'll show you where to add the exit function. ----------------------------------------
There is no Knowledge, That is not power.

Yes, i'm a MySQL Newbie.
Age: 16
E-mail: projectnet01@cs.com
Company:(not done yet) :)
-Aaron
----------------------------------------
 
#!/usr/local/bin/perl
print &quot;Content-type: text/html\n\n&quot;;

require &quot;yahtzee.cgi&quot;or &error(&quot;Data File is missing!&quot;,&quot;Error&quot;);

sub error {
my($error, $title) = @_;
print &quot;Error: $error<BR>&quot;;
exit;
}

#still not sure what this part does exactly
#just know it works

read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/,$input);
foreach $pair(@pairs){
($name, $value) = split(/=/,$pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;
$value =~ s/\n/ /g;
$value =~ s/\r//g;
$FORM{$name} = $value;
$mode = $FORM{'mode'};

}

print <<EOF;
<form method=&quot;POST&quot; action=&quot;roll.cgi&quot;>
EOF

if ($FORM{'C1'} eq &quot;ON&quot;) {
print <<EOF;
Dice one rolled
<input type=&quot;checkbox&quot; name=&quot;C1&quot; value=&quot;ON&quot;>
EOF

&dice;
$dice1 = $num[0];
print &quot;<br>&quot;;
}
else {
#Continue
}

if ($FORM{'C2'} eq &quot;ON&quot;) {
print <<EOF;
Dice two rolled
<input type=&quot;checkbox&quot; name=&quot;C2&quot; value=&quot;ON&quot;>
EOF

&dice;
$dice2 = $num[0];
print &quot;<br>&quot;;
}
else {
#Continue
}

if ($FORM{'C3'} eq &quot;ON&quot;) {
print <<EOF;
Dice three rolled
<input type=&quot;checkbox&quot; name=&quot;C3&quot; value=&quot;ON&quot;>
EOF

&dice;
$dice3 = $num[0];
print &quot;<br>&quot;;
}
else {
#Continue
}

if ($FORM{'C4'} eq &quot;ON&quot;) {
print <<EOF;
Dice four rolled
<input type=&quot;checkbox&quot; name=&quot;C4&quot; value=&quot;ON&quot;>
EOF

&dice;
$dice4 = $num[0];
print &quot;<br>&quot;;
}
else {
#Continue
}

if ($FORM{'C5'} eq &quot;ON&quot;) {
print <<EOF;
Dice five rolled
<input type=&quot;checkbox&quot; name=&quot;C5&quot; value=&quot;ON&quot;>
EOF

&dice;
$dice5 = $num[0];
print &quot;<br>&quot;;
}
else {
#Continue
}

print <<EOF;
<input type=&quot;submit&quot; value=&quot;Roll Selected Dice Again&quot; name=&quot;B1&quot;>
</form>
EOF
 
Sorry if i'm geting back to you late. I had school and stuff but it seems yo're having problem with the yahtzee.cgi. Can you email me the whole code in a zip file. I don't see any problems with the code listed above.

projectnet01@cs.com ----------------------------------------
There is no Knowledge, That is not power.

Yes, i'm a MySQL Newbie.
Age: 16
E-mail: projectnet01@cs.com
Company:(not done yet) :)
-Aaron
----------------------------------------
 
Sorry danieleloff@hotmail.com. Your mail to projectnet01 could not be delivered
because projectnet01 is not accepting mail with attachments or embedded images


#!/usr/local/bin/perl
#Yahtzee

print &quot;Content-type: text/html\n\n&quot;;

&roll;

sub roll {
print <<EOF;
<form method=&quot;POST&quot; action=&quot;roll.cgi&quot;>
Dice one rolled
<input type=&quot;checkbox&quot; name=&quot;C1&quot; value=&quot;ON&quot;>
EOF

&dice;
$dice1 = $num[0];
print &quot;<br>&quot;;

print <<EOF;
Dice two rolled
<input type=&quot;checkbox&quot; name=&quot;C2&quot; value=&quot;ON&quot;>
EOF

&dice;
$dice2 = $num[0];
print &quot;<br>&quot;;

print <<EOF;
Dice three rolled
<input type=&quot;checkbox&quot; name=&quot;C3&quot; value=&quot;ON&quot;>
EOF

&dice;
$dice3 = $num[0];
print &quot;<br>&quot;;

print <<EOF;
Dice four rolled
<input type=&quot;checkbox&quot; name=&quot;C4&quot; value=&quot;ON&quot;>
EOF

&dice;
$dice4 = $num[0];
print &quot;<br>&quot;;

print <<EOF;
Dice five rolled
<input type=&quot;checkbox&quot; name=&quot;C5&quot; value=&quot;ON&quot;>
EOF

&dice;
$dice5 = $num[0];
print &quot;<br>&quot;;
print <<EOF;
<input type=&quot;submit&quot; value=&quot;Roll Selected Dice Again&quot; name=&quot;B1&quot;>
</form>
EOF

}

sub dice {
$rand = rand(5);

$rand += 1;

@num = split(/\./,$rand);

$round = substr($num[1],0,1);

if ($round > 4) {
$num[0] += 1;
print &quot;$num[0]&quot;;
}
else {
print &quot;$num[0]&quot;;
}
}

#add this to the end of script..
1;
#end of code
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top