Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Simple Perl

Reply
Thread Tools

Simple Perl

 
 
71Mach1
Guest
Posts: n/a
 
      08-15-2005
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????

 
Reply With Quote
 
 
 
 
Keith Keller
Guest
Posts: n/a
 
      08-15-2005
On 2005-08-15, 71Mach1 <(E-Mail Removed)> wrote:
>
> 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

--
http://www.velocityreviews.com/forums/(E-Mail Removed)-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom
see X- headers for PGP signature information

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      08-15-2005
71Mach1 wrote:
> 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

 
Reply With Quote
 
71Mach1
Guest
Posts: n/a
 
      08-15-2005
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;

 
Reply With Quote
 
71Mach1
Guest
Posts: n/a
 
      08-15-2005
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!

 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      08-16-2005

"Keith Keller" <(E-Mail Removed)-francisco.ca.us> wrote in message
news:(E-Mail Removed)-francisco.ca.us...
> On 2005-08-15, 71Mach1 <(E-Mail Removed)> wrote:
>>
>> 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");
>


A loop would also help:

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

Matt


 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-16-2005
71Mach1 wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 02-27-2011 11:00 PM
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
How to call a simple perl script from HTML without need of HTTPS but simple HTTP ? Wladimir Borsov Perl Misc 4 05-11-2006 09:29 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments