Training, Open Source computer languages

This is page ... utine.html

Our email: • Phone: 01144 1225 708225

For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
How pass a variable from a form to a subroutine

Posted by ability (ability), 28 October 2002
I'm new at this but I have a variable, lets call it $var1 and I need a form to pass $var1 to a subroutine in a script.

The form has a drop down menu ie

<select size="1" name="var1">
   <option value="1">1</option>
   <option value="2">2</option>

How do I pass what ever value the user selects from the dropdown menu so that $var1= whatever the drop down menu value is??

Posted by admin (Graham Ellis), 29 October 2002
I'll assume you're using "CGI" - the common gateway interface.  If so, you might like to use a module such as to collect all the values from your form using the param method; personally, if this is the only thing that I would be doing with in an application, I just add in my own collect_form subroutine:

sub collect_form {

my $buffer;
my $field;
my @fof;
my %form;

       $form{"_method"} = "POST";
} else {
       $buffer = $ENV{"QUERY_STRING"};
       $form{"_method"} = "GET";

@fof = split(/&/,$buffer);
foreach $field(@fof) {
       my ($name,$value) = split(/=/,$field);
       $name =~ tr/+/ /;
       $name =~ s/%(..)/pack("C",hex($1))/eg;
       $value =~ tr/+/ /;
       $value =~ s/%(..)/pack("C",hex($1))/eg;
       if ($form{$name}) {
               $form{$name} .= "\n$value";
       } else {
               $form{$name} = $value;



and then in my main code (to answer your question) I would write
%els = collect_form();
$var1 = $els{"var1"};

CGI / getting values from form is a huge subject, but this example is a good general case that picks up all the elements from a form if you've used the GET or POST method when setting up the form.  I have an enhanced version that feeds cookie variables and environment variables into the same hash as well to avoid the need to keep recoding.  After all, what are subs for?

Posted by John_Moylan (jfp), 29 October 2002
I'm a big fan of, and its simple to use too.

use CGI;
my $q = CGI->new();
my $var1 = $q->param('var1');
print "$var1\n";

what if your html select box allowed multiple selections?
my @var1 = $q->param('var1'); # it can handle arrays too


Posted by John_Moylan (jfp), 29 October 2002
Oh, on a related note.......

Don't trust input from users, they mess with things you know.

Get used to using the -T switch in your cgi progs, it makes you 'untaint' user input.

I'll leave it to you to read up on it.


This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho