2

I'm a beginner in programming, this is my first language. And in my class we are using a slightly out of date book to learn with (Book copyrighted '02). Doubt this would affect you helping me much, but worth noting.

The problem

I don't know how to format a simple foreach statement using/combined with an array. I'm getting mixed up and my book doesn't provide examples. I'm trying to get it so the Uses/Primary_Uses are shown when the user checks multiple checkboxes.

#!/usr/bin/perl
#c04ex5.cgi - creates a dynamic Web page that acknowledges
#the receipt of a registration form
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);
use strict;

#declare variables
my ($name, $serial, $modnum, $sysletter, $primary_uses, $use, @primary_uses, @uses);
my @models = ("Laser JX", "Laser PL", "ColorPrint XL");
my @systems = ("Windows", "Macintosh", "UNIX"); 
my @primary_uses = ("Home", "Business", "Educational", "Other");


#assign input items to variables
$name = param('Name');
$serial = param('Serial');
$modnum = param('Model');
$sysletter = param('System');
@primary_uses = param('Use');



#create Web page
print "<HTML><HEAD><TITLE>Juniper Printers</TITLE></HEAD>\n";
print "<BODY><H2>\n";
print "Thank you , $name, for completing \n";
print "the registration form.<BR><BR>\n";
print "We have registered your Juniper $models[$modnum] printer, \n";
print "serial number $serial.\n";
print "You indicated that the printer will be used on the\n";
print "$systems[$sysletter] system. <BR>\n";
print "The primary uses for this printer will be the following:\n";

#The part I'm having trouble with.     
foreach $use (@primary_uses) {
print "$use [@use]<BR>\n";
} 

print "</H2></BODY></HTML>\n";

My naming of variables might be a bit off, I was getting desperate and making sure I declare more than I should.

3
  • Do you just want to print all the elements in array @primary_uses? Commented Mar 9, 2014 at 8:12
  • You should reduce your code to only show your problem area. This is far too much code. Also, you should always use warnings. Commented Mar 9, 2014 at 8:13
  • Sorry, I didn't know the etiquette of posting for coding problems. I will remember to only post the part I'm having trouble with. Commented Mar 9, 2014 at 19:36

2 Answers 2

2

If you wanted to print a simple list of items, you should just use the $use variable:

foreach $use (@primary_uses) {
    print "$use<BR>\n";
} 

Note that this will also remove the fatal error that comes from not declaring @use. Perhaps that was also a point of confusion for you. $use and @use are two completely different variables, despite having the same name.

Note that you can print a list with the CGI module very easily:

my $cgi = CGI->new;
print $cgi->li(\@primary_uses);

Outputs the list interpolated in a list html entity, like so:

<li>Home</li> <li>Business</li> <li>Educational</li> <li>Other</li>

Some other pointers:

Note that it is a good idea to declare your variables in the smallest scope possible

foreach my $use (@primary_uses) {   # note the use of "my"
    print "$use<BR>\n";
} 

That also goes with the other variables. A good idea is to declare them right as you initialize them:

my $name = param('Name');

Then people who read your code don't have to scan backwards in the file to see where the variable has "been" before.

Note that you should never, ever use the content of data from a web form without sanitizing it first, because it is a huge security risk, especially when you print it. It allows a web user to execute arbitrary code on your system.

You should know that for and foreach are aliases for the same function.

Also, you should always, always use warnings:

use warnings;

There really is no good reason to ever not turn warnings on.

Sign up to request clarification or add additional context in comments.

Comments

0
foreach my $myuse (@primary_uses) {
    print $myuse;
}

You need to declare the variable.

6 Comments

You have the right answer, just the wrong explanation.
Right, $use is declared as a scalar, not as an array.
Nope... $use and @use are two different variables, and its perfectly legal to use the same name. And also he declares @uses, not @use (which also leads to a strict-error)
In the foreach loop she used @use which is not declared - this would cause an error since use strict is set
Yes, she could have declared both but she didn't
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.