473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Passing parameters to subroutine?

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
16 2769
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
RonB
589 Expert Mod 512MB
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
@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
RonB
589 Expert Mod 512MB
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
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
RonB
589 Expert Mod 512MB
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
RonB
589 Expert Mod 512MB
$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
I figured it out. Thanks anyway!
Jun 23 '09 #9
KevinADC
4,059 Expert 2GB
@JWest46088
ouch .....
Jun 24 '09 #10
RonB
589 Expert Mod 512MB
@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
@KevinADC
Why "ouch"? What do you mean?
Jun 24 '09 #12
KevinADC
4,059 Expert 2GB
@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
RonB
589 Expert Mod 512MB
@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
@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
3,509 Expert Mod 2GB
@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
RonB
589 Expert Mod 512MB
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

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

Similar topics

6
by: Adrian | last post by:
I am trying to pass the address of a C++ function into a Fortran routine to enable the Fortran routine to call this C++ function. I have to do it this way as our build process does not allow...
1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
3
by: Scott | last post by:
What is the proper syntax for sending an argument from a form contol to a subroutine in a module? For instance, from a textbox on a form I call a module subroutine from the textbox's OnUpdate...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
7
by: Richard Grant | last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter How do I do that in VB .NET What should be the syntax for a parameter to receive the address of a...
3
by: Stephen Travis | last post by:
I'm trying to write a subroutine that will fill an array of some type with several objects of that type. The subroutine works fine if I directly reference the array but if I pass the array as an...
2
by: Mark Drummond | last post by:
Hi all. I've been using Perl for many years now, but I am a "use it and learn it as you need it" type. I having some trouble passing a list to the "search" subroutine from Net::LDAP. I am trying...
14
by: bgreenspan | last post by:
I have a form with a subform. In the subform I have several fields to which I am assigning links to documents on the file server. I learned (from this list :-) how to add the links. Because I...
2
by: mj.redfox.mj | last post by:
Hi, Pretty basic question, apologies but being a bit of a newbie I still don't know the answer to this kind of thing! I have a repeater which, upon databind calls a subroutine, as below: ...
2
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p *...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.