Connecting Tech Pros Worldwide Forums | Help | Site Map

Passing parameters to subroutine?

Member
 
Join Date: Sep 2006
Posts: 68
#1: Jun 23 '09
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:

Expand|Select|Wrap|Line Numbers
  1. # 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.
  2.  
  3. .
  4. .
  5. .
  6.  
  7. $buffer = $ENV{'QUERY_STRING'};
  8.  
  9. if($FORM{'form'} == 2)
  10. {
  11.         &form2;         
  12.         exit;
  13. }
  14. elsif($FORM{'form'} == 3 || $FORM{'form'} == 4)
  15. {
  16. #      &form3($initial, $increment, $numvalues, $numcols);
  17.         &form3;
  18.         exit;
  19. }        
  20. else
  21. {
  22.         &form1;         
  23.         exit;
  24. }
  25.  
  26. sub form1 {
  27. print "<form method='get' value=$url>";
  28. print "<input type='hidden' name='form' value=2>";
  29.  
  30. print "Enter # to increment: ";
  31. print "<input type='text' name='initial' value=2>";
  32. print "Enter # to increment by: ";
  33. print "<input type='text' name='increment' value=3>";
  34. print "Enter # of times to increment: ";
  35. print "<input type='text' name='numvalues' value=10>";
  36. print "<input type='submit' name='submit' value='Submit'>";
  37. }
  38.  
  39. sub form2 {
  40.  
  41. # sub form2 receives the 3 variables (initial, increment, numvalues)
  42.  
  43. print "<form method='get' value=$url>";
  44.  
  45. print "Enter number of columns: ";
  46. print "<input type='text' name='numcols' value=3>";
  47. print "<input type='submit' name='submit' value='Submit'>";
  48. }
  49.  
  50. sub form3 {
  51.  
  52. # sub form3 does not receive any of the variables (initial, increment, numvalues, numcols)
  53.  
  54. print "<form method='get' value=$url>";
  55.  
  56. print "test"; # test prints out
  57.  
  58. print $initial, $increment, $numvalues, $numcols; #nothing prints out
  59. }

Member
 
Join Date: Sep 2006
Posts: 68
#2: Jun 23 '09

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
#3: Jun 23 '09

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
#4: Jun 23 '09

re: Passing parameters to subroutine?


Quote:

Originally Posted by RonB View Post

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:

Expand|Select|Wrap|Line Numbers
  1. $initial = $FORM{'initial'};
  2. $increment = $FORM{'increment'};
  3. $numvalues = $FORM{'numvalues'};
  4. $numcols = $FORM{'numcols'};
  5.  
Member
 
Join Date: Jun 2009
Posts: 55
#5: Jun 23 '09

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.
Expand|Select|Wrap|Line Numbers
  1. use strcit;
  2. use warnings;
  3.  
CGI scripts that process form data should include this:
Expand|Select|Wrap|Line Numbers
  1. use CGI;
While debugging cgi scripts, you should include this:
Expand|Select|Wrap|Line Numbers
  1. use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
and immediately after printing the header, you do this:
Expand|Select|Wrap|Line Numbers
  1. warningsToBrowser(1);
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.

Expand|Select|Wrap|Line Numbers
  1. use CGI;
  2.  
  3. my $cgi = CGI->new;
  4. my %form = $cgi->Vars;
  5.  
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
#6: Jun 23 '09

re: Passing parameters to subroutine?


Here is my complete code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. BEGIN
  4. {
  5.         open(STDERR, ">&STDOUT");
  6.         select(STDERR); $| = 1;
  7.         select(STDOUT); $| = 1;
  8.         print qq{Content-type: text/html\n\n
  9.         <title>Assignment #5b (CS Credit)</title>
  10.         <body text="white" bgcolor="blue">
  11.         <font face="arial" size="4">
  12.         <div align="center">};
  13. }
  14.  
  15. my $url = '/~westj2/cgi-bin/assignment5b_CS_credit.cgi';
  16.  
  17. $buffer = $ENV{'QUERY_STRING'};
  18. $ENV{'PATH_INFO'} = '/$initial/$increment/$numvalues/$numcols/';
  19.  
  20. @pairs = split(/&/, $buffer);
  21. foreach $pair (@pairs)
  22. {
  23.         ($name, $value) = split(/=/, $pair);
  24.         $value =~ tr/+/ /;
  25.         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  26.         $name =~ tr/+/ /;
  27.         $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  28.  
  29.         if($FORM{$name} eq "" )
  30.         {
  31.                 $FORM{$name} = $value;
  32.         }
  33.         else
  34.         {
  35.                 $FORM{$name}  .= " " . $value;
  36.         }
  37. }
  38. $initial = $FORM{'initial'};
  39. $increment = $FORM{'increment'};
  40. $numvalues = $FORM{'numvalues'};
  41. $numcols = $FORM{'numcols'};
  42.  
  43. if($FORM{'form'} == 2)
  44. {
  45.         &form2($initial, $increment, $numvalues);
  46. #       &form2;
  47.         exit;  
  48. }
  49. elsif($FORM{'form'} == 3 || $FORM{'form'} == 4)
  50. {
  51. #       &form3($initial, $increment, $numvalues, $numcols);
  52.         &form3;
  53.         exit;
  54. }
  55. else
  56. {
  57.         &form1;
  58.         exit;
  59. }
  60.  
  61. sub form3
  62. {
  63.         print "<FORM METHOD='GET' ACTION='$url/$intitial/$increment/$numvalues/$numcols/'>";
  64. .              
  65. .
  66. .
  67. }
  68.  
  69. sub form1
  70. {
  71.         print qq{
  72.         <form method="get" action="$url">
  73.         <input type="hidden" name="form" value=2><br>
  74.  
  75.         <table align='center'>
  76.         <tr>
  77.         <td><b><font face='arial' color='white' size='3'>
  78.         Enter a number to increment:
  79.         </td>
  80.         <td><input type='text' name='initial' value='2'></td>
  81.         </tr>
  82.         <br>
  83.         <tr>
  84.         <td><b><font face='arial' color='white' size='3'>
  85.         Enter an amount to increment by:
  86.         </td>
  87.         <td><input type='text' name='increment' value='3'></td>
  88.         </tr>
  89.         <br>
  90.         <tr>
  91.         <td><b><font face='arial' color='white' size='3'>
  92.         Enter number of times to increment:
  93.         </td>
  94.         <td><input type='text' name='numvalues' value='10'></td>
  95.         </tr>
  96.         </table>
  97.         <br>
  98.         <br>
  99.         <p align='center'>  
  100.         <input type='submit' name='submit' value='Submit'>
  101.         <input type='reset'>
  102.         </FORM>};
  103. }
  104.  
  105. sub form2
  106. {
  107.         my @nums = @_;
  108.  
  109.         print qq{
  110.         <form method='get' action='/~westj2/cgi-bin/assignment5b_CS_credit.cgi/initial=$nums[0]/increment=$nums[1]/numvalues=$nums[2]'>
  111.         <input type="hidden" name="form" value=3>
  112.         <br><br>
  113.         Number of columns:
  114.         <input type='text' name='numcols' value='3'>
  115.         <br><br>
  116.         <input type='submit' name='submit' value='Submit'>
  117.         <input type='reset'>
  118.         </FORM>};
  119. #       &form3($initial, $increment, $numvalues, $numcols);
  120. }
  121.  
Member
 
Join Date: Jun 2009
Posts: 55
#7: Jun 23 '09

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
#8: Jun 23 '09

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
#9: Jun 23 '09

re: Passing parameters to subroutine?


I figured it out. Thanks anyway!
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#10: Jun 24 '09

re: Passing parameters to subroutine?


Quote:

Originally Posted by JWest46088 View Post

I figured it out. Thanks anyway!

ouch .....
Member
 
Join Date: Jun 2009
Posts: 55
#11: Jun 24 '09

re: Passing parameters to subroutine?


Quote:

Originally Posted by KevinADC View Post

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
#12: Jun 24 '09

re: Passing parameters to subroutine?


Quote:

Originally Posted by KevinADC View Post

ouch .....

Why "ouch"? What do you mean?
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#13: Jun 24 '09

re: Passing parameters to subroutine?


Quote:

Originally Posted by JWest46088 View Post

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
#14: Jun 24 '09

re: Passing parameters to subroutine?


Quote:

Originally Posted by KevinADC View Post

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
#15: Jun 24 '09

re: Passing parameters to subroutine?


Quote:

Originally Posted by KevinADC View Post

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.
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#16: Jun 25 '09

re: Passing parameters to subroutine?


Quote:

Originally Posted by JWest46088 View Post

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
#17: Jun 25 '09

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.
Reply