Simple Perl

7

71Mach1

Hello, I recently created a simple form via a .pl script.

Here it is:
#!/usr/bin/perl
# cgi-bin/ice-cream: program to answer *and generate* ice cream
# CV Form Version 2.0: Uses more while/for/foreach loops to get the
job done.
# Created: 8/15/05 RH
use strict; # enforce variable declarations and quoting
use CGI qw:)standard);

print header, start_html("Carrier Validation Test Architecture"),
h1("Carrier Validation Test Architecture");
if (param()) { # the form has already been filled out
my $date = param("date");
my $Carrier = param("Carrier");
my $T1 = param("T1");
my $T2 = param("T2");
my $T3 = param("T3");
my $T4 = param("T4");
my $T5 = param("T5");
my $T6 = param("T6");
my $T7 = param("T7");
my $T8 = param("T8");
my $T9 = param("T9");
my $T10 = param("T10");
my $T11 = param("T11");
my $T12 = param("T12");
my $T13 = param("T13");
my $T14 = param("T14");
my $T15 = param("T15");
my $T16 = param("T16");
my $T17 = param("T17");
my $T18 = param("T18");
my $T19 = param("T19");
my $T20 = param("T20");


my
$TESTS=($T1,$T2,$T3,$T4,$T5,$T6,$T7,$T8,$T9,$T10,$T11,$T12,$T13,$T14,$T15,$T16,$T17,$T18,$T19,$T20);
my $NUM = 1;
my $EXPR = /a-zA-Z0-9/;

print p("$date");
print p("$Carrier");


print p("@TESTS");
my $count = 0;

foreach my $TEST (@TESTS) {
if ($TEST[$count] != 'EMPTY') {
print p( "$NUM",". ","$TEST",checkbox( -NAME=> "P"),checkbox(
-NAME=>"F"));
$NUM++;
$count++;
}}

} else {# FIRST TIME THROUGH, PRESENT A CLEAN FORM
print hr(); # draw a horizontal rule before the form
print start_form();
print p("What is the date? ", textfield("date"));
print p("Carrier: ", popup_menu("Carrier", ['A','B','C','T-MobileUK',
'D', 'E']));
print p("What is the Suite Title? ", textfield( -NAME=> 'SuiteTitle',
-SIZE => 65));
print p("Enter the test case names of the suite you would like to have
reviewed");

print p(" 1. ", textfield( -NAME=> "T1", -SIZE => 65));
print p(" 2. ", textfield( -NAME=> "T2", -SIZE => 65));
print p(" 3. ", textfield( -NAME=> "T3", -SIZE => 65));
print p(" 4. ", textfield( -NAME=> "T4", -SIZE => 65));
print p(" 5. ", textfield( -NAME=> "T5", -SIZE => 65));
print p(" 6. ", textfield( -NAME=> "T6", -SIZE => 65));
print p(" 7. ", textfield( -NAME=> "T7", -SIZE => 65));
print p(" 8. ", textfield( -NAME=> "T8", -SIZE => 65));
print p(" 9. ", textfield( -NAME=> "T9", -SIZE => 65));
print p("10. ", textfield( -NAME=> "T10", -SIZE => 65));
print p("11. ", textfield( -NAME=> "T11", -SIZE => 65));
print p("12. ", textfield( -NAME=> "T12", -SIZE => 65));
print p("13. ", textfield( -NAME=> "T13", -SIZE => 65));
print p("14. ", textfield( -NAME=> "T14", -SIZE => 65));
print p("15. ", textfield( -NAME=> "T15", -SIZE => 65));
print p("16. ", textfield( -NAME=> "T16", -SIZE => 65));
print p("17. ", textfield( -NAME=> "T17", -SIZE => 65));
print p("18. ", textfield( -NAME=> "T18", -SIZE => 65));
print p("19. ", textfield( -NAME=> "T19", -SIZE => 65));
print p("20. ", textfield( -NAME=> "T20", -SIZE => 65));
print p("Submit for verification ", popup_menu("SUBMIT", [ 1..3 ]));
print p(submit("SUBMIT"), reset("CLEAR"));
print end_form(), hr();
}
print end_html;


As you can see, I'm assigning all the textfield values to @TESTS and
then I'm trying to seperate them out and only print $NUM and $TEST
while values of the array are not empty. You can see that what I am
trying to do is to only print out while the $TEST value is not equal to
"". I've tried several if statments to try to get it and I've browsed
all of the 3 books that I have but I can't find a solution to NOT print
if the array value equals blank.

Any ideas????
 
K

Keith Keller

Here it is:
#!/usr/bin/perl
# cgi-bin/ice-cream: program to answer *and generate* ice cream
# CV Form Version 2.0: Uses more while/for/foreach loops to get the
job done.
# Created: 8/15/05 RH
use strict; # enforce variable declarations and quoting

That's nice, but when I run your code I get:

Global symbol "@TESTS" requires explicit package name at perl/clpmisc
line 46.
Global symbol "@TESTS" requires explicit package name at perl/clpmisc
line 49.
Global symbol "@TEST" requires explicit package name at perl/clpmisc
line 50.

I'd fix these errors before moving on. (And also use warnings; while
the code is in development.)
my $T1 = param("T1");
my $T2 = param("T2");
my $T3 = param("T3");
my $T4 = param("T4");
my $T5 = param("T5");
my $T6 = param("T6");
my $T7 = param("T7");
my $T8 = param("T8");
my $T9 = param("T9");
my $T10 = param("T10");
my $T11 = param("T11");
my $T12 = param("T12");
my $T13 = param("T13");
my $T14 = param("T14");
my $T15 = param("T15");
my $T16 = param("T16");
my $T17 = param("T17");
my $T18 = param("T18");
my $T19 = param("T19");
my $T20 = param("T20");

It'd be nicer to use an array or hash for these values:

my %testHash;
$testHash{'t1'} = param("T1");
my
$TESTS=($T1,$T2,$T3,$T4,$T5,$T6,$T7,$T8,$T9,$T10,$T11,$T12,$T13,$T14,$T15,$T16,$T17,$T18,$T19,$T20);

This line is *not* doing what you think it's doing. () makes a list, so
you want to assign to an array, not a scalar.

It's also customary not to use all caps for variable names; they are
typically used for built-in variables. my @tests will look more
idiomatic than my @TESTS.

--keith
 
P

Paul Lalli

71Mach1 said:
Hello, I recently created a simple form via a .pl script.

Here it is:

I don't believe this is the code you're actually using. Because when I
run it, I get:
Global symbol "@TESTS" requires explicit package name at ./clpm.pl line
42.
Global symbol "@TESTS" requires explicit package name at ./clpm.pl line
45.
Global symbol "@TEST" requires explicit package name at ./clpm.pl line
46.
Execution of ./clpm.pl aborted due to compilation errors.

Please copy and paste your code. Do not attempt to retype it.
#!/usr/bin/perl
# cgi-bin/ice-cream: program to answer *and generate* ice cream
# CV Form Version 2.0: Uses more while/for/foreach loops to get the
job done.
# Created: 8/15/05 RH
use strict; # enforce variable declarations and quoting

Very good that you're using strict.
Very BAD that you're not using warnings.
use CGI qw:)standard);

print header, start_html("Carrier Validation Test Architecture"),
h1("Carrier Validation Test Architecture");
if (param()) { # the form has already been filled out
my $date = param("date");
my $Carrier = param("Carrier");
my $T1 = param("T1");
my $T2 = param("T2");
my $T3 = param("T3");
my $T4 = param("T4");
my $T5 = param("T5");
my $T6 = param("T6");
my $T7 = param("T7");
my $T8 = param("T8");
my $T9 = param("T9");
my $T10 = param("T10");
my $T11 = param("T11");
my $T12 = param("T12");
my $T13 = param("T13");
my $T14 = param("T14");
my $T15 = param("T15");
my $T16 = param("T16");
my $T17 = param("T17");
my $T18 = param("T18");
my $T19 = param("T19");
my $T20 = param("T20");

This is horrendous. For one, your variable names should have some kind
of meaning. For two, if you find yourself naming variables with
sequential numbers, you should be using an array.
my
$TESTS=($T1,$T2,$T3,$T4,$T5,$T6,$T7,$T8,$T9,$T10,$T11,$T12,$T13,$T14,$T15,$T16,$T17,$T18,$T19,$T20);

Here's the root of the first compilation error. I'm guessing that's
supposed to be
my @TESTS = (...);
my $NUM = 1;
my $EXPR = /a-zA-Z0-9/;

I somehow dobut this is doing what you think it's doing. This is
assigning $EXPR to a true or false value, depending on whether or not
$_ matches that regexp. Of course, you never use $EXPR again, so it
doesn't much matter anyway. 'use warnings;' would have told you about
this, of course.
print p("$date");
print p("$Carrier");

Useless use of double quotes. Please read:
perldoc -q quoting
print p("@TESTS");
my $count = 0;

foreach my $TEST (@TESTS) {
if ($TEST[$count] != 'EMPTY') {

Firstly, this looping construct is nonsensical. You are looping
through @TESTS, assigning each element to $TEST, but then don't use the
variable you just created. You instead keep separate track of a
counter variable, and use array-index notation to get at the element
you want. Choose one method, don't try to combine them.

Either:
for my $TEST (@TESTS){
print "The element is $TEST\n"
}

Or:
for (my $count = 0; $count < @TESTS; $count++){
print "The element is $TESTS[$count]\n";
}

Or:
for my $count (0..$#TESTS){
print "The element is $TESTS[$count]\n";
}

Personally, the first option looks the best to me.

Secondly, you are using the numerical comparison operator to compare
strings. This is not what you want to do. != will convert both
arguments to numbers. 'EMPTY' becomes the number 0. $TEST[$count]
will become 0 if it starts with anything that doesn't "look" like a
number. Otherwise, it will become the number represented by the
maximum sequence of characters (starting with the first) that can
represent a numeric value.

To compare strings, use the 'ne' operator, not the '!=' operator.
print p( "$NUM",". ","$TEST",checkbox( -NAME=> "P"),checkbox( -NAME=>"F"));

Ugh. In addition to the useless quotes again, why are you bothering to
separate those first three strings?
print p("$NUM. $TEST", checkbox(...), checkbox(...));

(Oh, and you're back to using $TEST again.)
$NUM++;
$count++;
}}

} else {# FIRST TIME THROUGH, PRESENT A CLEAN FORM
print hr(); # draw a horizontal rule before the form
print start_form();
print p("What is the date? ", textfield("date"));
print p("Carrier: ", popup_menu("Carrier", ['A','B','C','T-MobileUK',
'D', 'E']));
print p("What is the Suite Title? ", textfield( -NAME=> 'SuiteTitle',
-SIZE => 65));
print p("Enter the test case names of the suite you would like to have
reviewed");

print p(" 1. ", textfield( -NAME=> "T1", -SIZE => 65));
print p(" 2. ", textfield( -NAME=> "T2", -SIZE => 65));
print p(" 3. ", textfield( -NAME=> "T3", -SIZE => 65));
print p(" 4. ", textfield( -NAME=> "T4", -SIZE => 65));
print p(" 5. ", textfield( -NAME=> "T5", -SIZE => 65));
print p(" 6. ", textfield( -NAME=> "T6", -SIZE => 65));
print p(" 7. ", textfield( -NAME=> "T7", -SIZE => 65));
print p(" 8. ", textfield( -NAME=> "T8", -SIZE => 65));
print p(" 9. ", textfield( -NAME=> "T9", -SIZE => 65));
print p("10. ", textfield( -NAME=> "T10", -SIZE => 65));
print p("11. ", textfield( -NAME=> "T11", -SIZE => 65));
print p("12. ", textfield( -NAME=> "T12", -SIZE => 65));
print p("13. ", textfield( -NAME=> "T13", -SIZE => 65));
print p("14. ", textfield( -NAME=> "T14", -SIZE => 65));
print p("15. ", textfield( -NAME=> "T15", -SIZE => 65));
print p("16. ", textfield( -NAME=> "T16", -SIZE => 65));
print p("17. ", textfield( -NAME=> "T17", -SIZE => 65));
print p("18. ", textfield( -NAME=> "T18", -SIZE => 65));
print p("19. ", textfield( -NAME=> "T19", -SIZE => 65));
print p("20. ", textfield( -NAME=> "T20", -SIZE => 65));

Insanity.

for (1..20){
print p("$_. ", textfield(-name=>'testcases', -size=>65));
}

And then when you want to grab all the values later:
my @testcases = param('testcases');

print p("Submit for verification ", popup_menu("SUBMIT", [ 1..3 ]));
print p(submit("SUBMIT"), reset("CLEAR"));
print end_form(), hr();
}
print end_html;


As you can see, I'm assigning all the textfield values to @TESTS and
then I'm trying to seperate them out and only print $NUM and $TEST
while values of the array are not empty.

First of all, the string 'EMPTY' is not the same as an empty string.
The string 'EMPTY' has five characters. An empty string has zero
characters.
You can see that what I am
trying to do is to only print out while the $TEST value is not equal to
"".

No, I didn't see that at all. I saw you comparing $TESTS[$count]
(numerically) to the string 'EMPTY'.

If you want to compare $TEST to '', then do that:

if ($TEST ne '') {
print "$TEST does not contain the empty string\n";
}
I've tried several if statments to try to get it and I've browsed
all of the 3 books that I have but I can't find a solution to NOT print
if the array value equals blank.

I fail to believe that. What books were you using that didn't teach
you the proper use of either strings or the equality operators?
Any ideas????

Yes. You need to read more carefully the documentation.

perldoc perlop
perldoc perldata

would be good places to start.

Hope the advise contained in this message is helpful,
Paul Lalli
 
7

71Mach1

Here's the exact script below. I'm running a .pl program which is
located on a web server. I'm running it with IE and Firefox.

#!/usr/bin/perl
#
# CV Form Version 2.0: Uses more while/for/foreach loops to get the
job done.
# Created: 8/15/05 RH
use strict; # enforce variable declarations and quoting
use CGI qw:)standard);

print header, start_html("Carrier Validation Test Architecture"),
h1("Carrier Validation Test Architecture");
if (param()) { # the form has already been filled out
my $date = param("date");
my $Carrier = param("Carrier");
my $T1 = param("T1");
my $T2 = param("T2");
my $T3 = param("T3");
my $T4 = param("T4");
my $T5 = param("T5");
my $T6 = param("T6");
my $T7 = param("T7");
my $T8 = param("T8");
my $T9 = param("T9");
my $T10 = param("T10");
my $T11 = param("T11");
my $T12 = param("T12");
my $T13 = param("T13");
my $T14 = param("T14");
my $T15 = param("T15");
my $T16 = param("T16");
my $T17 = param("T17");
my $T18 = param("T18");
my $T19 = param("T19");
my $T20 = param("T20");


my
@TESTS=($T1,$T2,$T3,$T4,$T5,$T6,$T7,$T8,$T9,$T10,$T11,$T12,$T13,$T14,$T15,$T16,$T17,$T18,$T19,$T20);
my $NUM = 1;


print p("$date");
print p("$Carrier");
# print p("1. $T1",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("2. $T2",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("3. $T3",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("4. $T4",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("5. $T5",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("6. $T6",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("7. $T7",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("8. $T8",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("9. $T9",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("10. $T10",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("11. $T11",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("12. $T12",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("13. $T13",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("14. $T14",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("15. $T15",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("16. $T16",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("17. $T17",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("18. $T18",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("19. $T19",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));
# print p("20. $T20",checkbox( -NAME=> "P"),checkbox( -NAME=> "F"));

print p("@TESTS");
my $count = 0;

foreach my $TEST (@TESTS) {
if ( defined $TEST) {

print p( "$NUM",". ","$TEST",checkbox( -NAME=> "P"),checkbox(
-NAME=>"F"));
$NUM++;
$count++;
}}

} else {# FIRST TIME THROUGH, PRESENT A CLEAN FORM
print hr(); # draw a horizontal rule before the form
print start_form();
print p("What is the date? ", textfield("date"));
print p("Carrier: ", popup_menu("Carrier",
['Cingular','O2','Orange','T-MobileUK', 'T-MobileUS', 'Vodafone']));
print p("What is the Suite Title? ", textfield( -NAME=> 'SuiteTitle',
-SIZE => 65));
print p("Enter the test case names of the suite you would like to have
reviewed");

print p(" 1. ", textfield( -NAME=> "T1",-SIZE => 65));
print p(" 2. ", textfield( -NAME=> "T2",-SIZE => 65));
print p(" 3. ", textfield( -NAME=> "T3",-SIZE => 65));
print p(" 4. ", textfield( -NAME=> "T4",-SIZE => 65));
print p(" 5. ", textfield( -NAME=> "T5",-SIZE => 65));
print p(" 6. ", textfield( -NAME=> "T6",-SIZE => 65));
print p(" 7. ", textfield( -NAME=> "T7",-SIZE => 65));
print p(" 8. ", textfield( -NAME=> "T8",-SIZE => 65));
print p(" 9. ", textfield( -NAME=> "T9",-SIZE => 65));
print p("10. ", textfield( -NAME=> "T10",-SIZE => 65));
print p("11. ", textfield( -NAME=> "T11",-SIZE => 65));
print p("12. ", textfield( -NAME=> "T12",-SIZE => 65));
print p("13. ", textfield( -NAME=> "T13",-SIZE => 65));
print p("14. ", textfield( -NAME=> "T14",-SIZE => 65));
print p("15. ", textfield( -NAME=> "T15",-SIZE => 65));
print p("16. ", textfield( -NAME=> "T16",-SIZE => 65));
print p("17. ", textfield( -NAME=> "T17",-SIZE => 65));
print p("18. ", textfield( -NAME=> "T18",-SIZE => 65));
print p("19. ", textfield( -NAME=> "T19",-SIZE => 65));
print p("20. ", textfield( -NAME=> "T20",-SIZE => 65));
print p("Submit for verification ", popup_menu("SUBMIT", [ 1..3 ]));
print p(submit("SUBMIT"), reset("CLEAR"));
print end_form(), hr();
}
print end_html;
 
7

71Mach1

Thanks PAUL! You hit the nail right on the head!
foreach my $TEST (@TESTS) {
if ($TEST ne "") {

print p( "$NUM",". ","$TEST",checkbox( -NAME=> "P"),checkbox(
-NAME=>"F"));
$NUM++;
}}

Replacing the != was the trick. Now this works great!! Many thanks!
 
M

Matt Garrish

Keith Keller said:
That's nice, but when I run your code I get:

Global symbol "@TESTS" requires explicit package name at perl/clpmisc
line 46.
Global symbol "@TESTS" requires explicit package name at perl/clpmisc
line 49.
Global symbol "@TEST" requires explicit package name at perl/clpmisc
line 50.

I'd fix these errors before moving on. (And also use warnings; while
the code is in development.)


It'd be nicer to use an array or hash for these values:

my %testHash;
$testHash{'t1'} = param("T1");

A loop would also help:

map { $testHash{"t$_"} = param("T$_"); } (1..20);

Matt
 
P

Paul Lalli

71Mach1 said:
Thanks PAUL!

You're welcome.
You hit the nail right on the head!
foreach my $TEST (@TESTS) {
if ($TEST ne "") {
print p( "$NUM",". ","$TEST",checkbox( -NAME=> "P"),checkbox(-NAME=>"F"));
$NUM++;
}
}

Replacing the != was the trick. Now this works great!! Many thanks!

I'm glad your immediate issue was solved. However, please do take to
heart the remainder of the suggestions in my original post. The most
important one is that you should have 'use warnings;' in your script.
That line would have told you that you were using != on a non-numeric
value, and saved you probably many hours of debugging and guessing.

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top