Passing parameters to subroutine? | Member | | Join Date: Sep 2006
Posts: 68
| |
I have three subroutines. sub form3 won't receive any of the variables and I don't know why. It is probably a simple solution, but I've tried everything! This is just a snippet of my code: - # This all has to be done using path info. I'm not quite sure how to do that. I did the same program using hidden variables, and it worked fine.
-
-
.
-
.
-
.
-
-
$buffer = $ENV{'QUERY_STRING'};
-
-
if($FORM{'form'} == 2)
-
{
-
&form2;
-
exit;
-
}
-
elsif($FORM{'form'} == 3 || $FORM{'form'} == 4)
-
{
-
# &form3($initial, $increment, $numvalues, $numcols);
-
&form3;
-
exit;
-
}
-
else
-
{
-
&form1;
-
exit;
-
}
-
-
sub form1 {
-
print "<form method='get' value=$url>";
-
print "<input type='hidden' name='form' value=2>";
-
-
print "Enter # to increment: ";
-
print "<input type='text' name='initial' value=2>";
-
print "Enter # to increment by: ";
-
print "<input type='text' name='increment' value=3>";
-
print "Enter # of times to increment: ";
-
print "<input type='text' name='numvalues' value=10>";
-
print "<input type='submit' name='submit' value='Submit'>";
-
}
-
-
sub form2 {
-
- # sub form2 receives the 3 variables (initial, increment, numvalues)
-
-
print "<form method='get' value=$url>";
-
-
print "Enter number of columns: ";
-
print "<input type='text' name='numcols' value=3>";
-
print "<input type='submit' name='submit' value='Submit'>";
-
}
-
-
sub form3 {
-
- # sub form3 does not receive any of the variables (initial, increment, numvalues, numcols)
-
-
print "<form method='get' value=$url>";
-
-
print "test"; # test prints out
-
-
print $initial, $increment, $numvalues, $numcols; #nothing prints out
-
}
| | Member | | Join Date: Sep 2006
Posts: 68
| | | re: Passing parameters to subroutine?
I guess what I'm actually asking is how to use path_info to get form3 to accept the variables ($initial, $increment, $numvalues).
| | Member | | Join Date: Jun 2009
Posts: 55
| | | re: Passing parameters to subroutine?
Since you haven't shown us where and how you're assigning those vars, we can't say why there is nothing being printed.
| | Member | | Join Date: Sep 2006
Posts: 68
| | | re: Passing parameters to subroutine? Quote:
Originally Posted by RonB Since you haven't shown us where and how you're assigning those vars, we can't say why there is nothing being printed. They are assigned like this right before the subroutine calls: -
$initial = $FORM{'initial'};
-
$increment = $FORM{'increment'};
-
$numvalues = $FORM{'numvalues'};
-
$numcols = $FORM{'numcols'};
-
| | Member | | Join Date: Jun 2009
Posts: 55
| | | re: Passing parameters to subroutine?
From the limited code you posted I can tell that you're not using the strict pragma or CGI module, and it's a very good bet that you haven't enabled warnings (with either the -w switch or the warnings pragma).
Every Perl script you write should have these 2 use statements. -
use strcit;
-
use warnings;
-
CGI scripts that process form data should include this:
While debugging cgi scripts, you should include this: - use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
and immediately after printing the header, you do this:
The combination of those items will tell you a lot of what's wrong.
Here's easiest and IMO the best method to import the form submission and put it into a hash. -
use CGI;
-
-
my $cgi = CGI->new;
-
my %form = $cgi->Vars;
-
strict - Perl pragma to restrict unsafe constructs http://search.cpan.org/~nwclark/perl.../lib/strict.pm
warnings - Perl pragma to control optional warnings http://search.cpan.org/~nwclark/perl...ib/warnings.pm
CGI - Handle Common Gateway Interface requests and responses http://search.cpan.org/~lds/CGI.pm-3.43/CGI.pm
CGI::Carp - CGI routines for writing to the HTTPD (or other) error log http://search.cpan.org/~lds/CGI.pm-3.43/CGI/Carp.pm | | Member | | Join Date: Sep 2006
Posts: 68
| | | re: Passing parameters to subroutine?
Here is my complete code: -
#!/usr/bin/perl
-
-
BEGIN
-
{
-
open(STDERR, ">&STDOUT");
-
select(STDERR); $| = 1;
-
select(STDOUT); $| = 1;
-
print qq{Content-type: text/html\n\n
-
<title>Assignment #5b (CS Credit)</title>
-
<body text="white" bgcolor="blue">
-
<font face="arial" size="4">
-
<div align="center">};
-
}
-
-
my $url = '/~westj2/cgi-bin/assignment5b_CS_credit.cgi';
-
-
$buffer = $ENV{'QUERY_STRING'};
-
$ENV{'PATH_INFO'} = '/$initial/$increment/$numvalues/$numcols/';
-
-
@pairs = split(/&/, $buffer);
-
foreach $pair (@pairs)
-
{
-
($name, $value) = split(/=/, $pair);
-
$value =~ tr/+/ /;
-
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
-
$name =~ tr/+/ /;
-
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
-
-
if($FORM{$name} eq "" )
-
{
-
$FORM{$name} = $value;
-
}
-
else
-
{
-
$FORM{$name} .= " " . $value;
-
}
-
}
-
$initial = $FORM{'initial'};
-
$increment = $FORM{'increment'};
-
$numvalues = $FORM{'numvalues'};
-
$numcols = $FORM{'numcols'};
-
-
if($FORM{'form'} == 2)
-
{
-
&form2($initial, $increment, $numvalues);
-
# &form2;
-
exit;
-
}
-
elsif($FORM{'form'} == 3 || $FORM{'form'} == 4)
-
{
-
# &form3($initial, $increment, $numvalues, $numcols);
-
&form3;
-
exit;
-
}
-
else
-
{
-
&form1;
-
exit;
-
}
-
-
sub form3
-
{
-
print "<FORM METHOD='GET' ACTION='$url/$intitial/$increment/$numvalues/$numcols/'>";
-
.
-
.
-
.
-
}
-
-
sub form1
-
{
-
print qq{
-
<form method="get" action="$url">
-
<input type="hidden" name="form" value=2><br>
-
-
<table align='center'>
-
<tr>
-
<td><b><font face='arial' color='white' size='3'>
-
Enter a number to increment:
-
</td>
-
<td><input type='text' name='initial' value='2'></td>
-
</tr>
-
<br>
-
<tr>
-
<td><b><font face='arial' color='white' size='3'>
-
Enter an amount to increment by:
-
</td>
-
<td><input type='text' name='increment' value='3'></td>
-
</tr>
-
<br>
-
<tr>
-
<td><b><font face='arial' color='white' size='3'>
-
Enter number of times to increment:
-
</td>
-
<td><input type='text' name='numvalues' value='10'></td>
-
</tr>
-
</table>
-
<br>
-
<br>
-
<p align='center'>
-
<input type='submit' name='submit' value='Submit'>
-
<input type='reset'>
-
</FORM>};
-
}
-
-
sub form2
-
{
-
my @nums = @_;
-
-
print qq{
-
<form method='get' action='/~westj2/cgi-bin/assignment5b_CS_credit.cgi/initial=$nums[0]/increment=$nums[1]/numvalues=$nums[2]'>
-
<input type="hidden" name="form" value=3>
-
<br><br>
-
Number of columns:
-
<input type='text' name='numcols' value='3'>
-
<br><br>
-
<input type='submit' name='submit' value='Submit'>
-
<input type='reset'>
-
</FORM>};
-
# &form3($initial, $increment, $numvalues, $numcols);
-
}
-
| | Member | | Join Date: Jun 2009
Posts: 55
| | | re: Passing parameters to subroutine?
Did you instructor tell you that you can't use Perl's de facto standard modules for cgi scripts or the warnings and strict pragmas?
| | Member | | Join Date: Jun 2009
Posts: 55
| | | re: Passing parameters to subroutine? Quote:
$ENV{'PATH_INFO'} = '/$initial/$increment/$numvalues/$numcols/';
Why are you doing that?
Since none of those vars have been assigned, $ENV{'PATH_INFO'} will end up haveing a value of:
' /////'
| | Member | | Join Date: Sep 2006
Posts: 68
| | | re: Passing parameters to subroutine?
I figured it out. Thanks anyway!
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | re: Passing parameters to subroutine? Quote:
Originally Posted by JWest46088 I figured it out. Thanks anyway! ouch .....
| | Member | | Join Date: Jun 2009
Posts: 55
| | | re: Passing parameters to subroutine? Quote:
Originally Posted by KevinADC ouch ..... Ya, I agree, but if (s)he wants to get a C minus (at best) on the assignment :-( , then I say "so be it".
| | Member | | Join Date: Sep 2006
Posts: 68
| | | re: Passing parameters to subroutine? Quote:
Originally Posted by KevinADC ouch ..... Why "ouch"? What do you mean?
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | re: Passing parameters to subroutine? Quote:
Originally Posted by JWest46088 Why "ouch"? What do you mean? After the time and energy that Ron put into helping you all he gets is a "Thanks anyway"? Maybe I am projecting, but I would take that as a backhanded compliment. I doubt you meant it that way, and I'm not saying you owe Ron or anyone anymore gratitude than you feel is appropriate, it was just rather funny that Ron gave you so much help and you gave him so little gratitude. The help and the gratitude are free so you may as well be generous. ;)
Then again, Ron is a filthy rich CEO of an evil corporation that takes advantage of children in third world nations so this could be karma in action***.
*** joke joke joke
| | Member | | Join Date: Jun 2009
Posts: 55
| | | re: Passing parameters to subroutine? Quote:
Originally Posted by KevinADC Then again, Ron is a filthy rich CEO of an evil corporation that takes advantage of children in third world nations so this could be karma in action***.
*** joke joke joke lol, I like that.
I wish I was "a filthy rich CEO", but the rest isn't far from the truth when you consider I work for the largest "Electronics Superstore" retailer in the US.
| | Member | | Join Date: Sep 2006
Posts: 68
| | | re: Passing parameters to subroutine? Quote:
Originally Posted by KevinADC After the time and energy that Ron put into helping you all he gets is a "Thanks anyway"? Maybe I am projecting, but I would take that as a backhanded compliment. I doubt you meant it that way, and I'm not saying you owe Ron or anyone anymore gratitude than you feel is appropriate, it was just rather funny that Ron gave you so much help and you gave him so little gratitude. The help and the gratitude are free so you may as well be generous. ;)
Then again, Ron is a filthy rich CEO of an evil corporation that takes advantage of children in third world nations so this could be karma in action***.
*** joke joke joke Ron, I apologize if I offended you in any way. I did not mean it to come off that way. I do appreciate the help you were trying to give me. After playing around with it a little more, I figured it out myself. I didn't use the advice you offered because, honestly, I didn't know how to. Once again, I apologize.
|  | Site Moderator | | Join Date: May 2007 Location: New Hampshire
Posts: 2,571
| | | re: Passing parameters to subroutine? Quote:
Originally Posted by JWest46088 Ron, I apologize if I offended you in any way. I did not mean it to come off that way. I do appreciate the help you were trying to give me. After playing around with it a little more, I figured it out myself. I didn't use the advice you offered because, honestly, I didn't know how to. Once again, I apologize. My suggestion is that if you don't know or understand what is being suggested, then say that. I would rather have someone tell me they don't understand so that I can maybe put my suggestions another way or provide more information that have them just say "Thanks, I figured it out" and go about their way. We are here to help and not trying to criticize. Our suggestions that seem like criticism are actually valid points, such as that pragmas that Ron suggested.... they are not required by Perl, but those of us who code Perl everyday make them a requirement because of what they do.
I would suggest you pick up the "Learning Perl" book. It is a great introductory tutorial into the world of Perl and will help with your questions. Also, the Perl FAQ will also help dramatically.
Regards,
Jeff
| | Member | | Join Date: Jun 2009
Posts: 55
| | | re: Passing parameters to subroutine?
Apology accepted. :)
If you have a good quality instructor, then (s)he will put comments in your code when grading so that you will better understand how to improve your code for the next assignment.
Feel free to ask us for additional help on your assignments, but keep in mind that we can/will guide you but not do your assignments for you.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|