By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,809 Members | 1,999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,809 IT Pros & Developers. It's quick & easy.

Passing parameters to subroutine?

P: 74
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. }
Jun 23 '09 #1
Share this Question
Share on Google+
16 Replies


P: 74
I guess what I'm actually asking is how to use path_info to get form3 to accept the variables ($initial, $increment, $numvalues).
Jun 23 '09 #2

Expert Mod 100+
P: 589
Since you haven't shown us where and how you're assigning those vars, we can't say why there is nothing being printed.
Jun 23 '09 #3

P: 74
@RonB
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.  
Jun 23 '09 #4

Expert Mod 100+
P: 589
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
Jun 23 '09 #5

P: 74
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.  
Jun 23 '09 #6

Expert Mod 100+
P: 589
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?
Jun 23 '09 #7

Expert Mod 100+
P: 589
$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:

'/////'
Jun 23 '09 #8

P: 74
I figured it out. Thanks anyway!
Jun 23 '09 #9

KevinADC
Expert 2.5K+
P: 4,059
@JWest46088
ouch .....
Jun 24 '09 #10

Expert Mod 100+
P: 589
@KevinADC
Ya, I agree, but if (s)he wants to get a C minus (at best) on the assignment :-( , then I say "so be it".
Jun 24 '09 #11

P: 74
@KevinADC
Why "ouch"? What do you mean?
Jun 24 '09 #12

KevinADC
Expert 2.5K+
P: 4,059
@JWest46088
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
Jun 24 '09 #13

Expert Mod 100+
P: 589
@KevinADC
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.
Jun 24 '09 #14

P: 74
@KevinADC
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.
Jun 24 '09 #15

numberwhun
Expert Mod 2.5K+
P: 3,503
@JWest46088
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
Jun 25 '09 #16

Expert Mod 100+
P: 589
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.
Jun 25 '09 #17

Post your reply

Sign in to post your reply or Sign up for a free account.