CGI.pm and foreach

S

shade

Greetings.

I am learning Perl, to enhance shell programming and generate web based
internal applications. I've been using CGI.pm, which is really nice.
However, I still find myself using <<HERE statements fairly often. Here is
what I am doing...

print $q->start_form;

print "<select name='order'>;
foreach $h in ( @heading ) {
print "<option>$h</option>";
}
print "</select>";

print $q->submit( -name=>'asc' -value=>'Ascending' ),
print $q->submit( -name=>'desc' -value=>'Descending'),
etc., etc.

I'm having trouble using a "scrolling_list" because of the foreach logic
that retrieves data for the drop down. Is there any way to do this in
CGI.pm, such as...

print $q->scrolling_list( -name=>'order' -value=>[
foreach ... ... ...
] ),

Or; would I be better off just using my own straight HTML when running
through logic. The script is working fine as is, I'm just trying to learn
how to do this the best way I can. i.e. with warnings, strict, taint and the
CGI.pm HTML parser.

Regards,
Shade
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

shade said:
I am learning Perl, to enhance shell programming and generate web
based internal applications. I've been using CGI.pm, which is really
nice. However, I still find myself using <<HERE statements fairly
often. Here is what I am doing...

print $q->start_form;

print "<select name='order'>;
foreach $h in ( @heading ) {
print "<option>$h</option>";
}

No, this is not what you're doing. This is full of syntax errors! In
the future, please copy/paste the code that you are referring to, rather
than re-typing it. It will save you time and effort and will help others
to help you better.

That said, the answer to your question is "it's a matter of style." I
personally have done quite a lot of CGI programming, and while I find the
CGI module absolutely indispensible, I prefer to generate my own HTML,
either with a templating mechanism or by using strings and print
statements. I personally feel that CGI.pm's html-generating operators
don't buy you a whole lot, and in some cases make things harder. I would
write a loop much like you have done above. Well, I would have used map,
but that again is a matter of taste.

However, as I said this is a matter of style. *Many* others feel
differently, and wouldn't write CGI programs without CGI.pm's HTML
generating functions/methods. Try both styles, decide which you prefer.

- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPy8UVWPeouIeTNHoEQKmHQCcCfMgML5n+tYm53TauQbB7ITLHhQAoObR
l4tQbmaonm3IlfO/7CEP4qR8
=tmhv
-----END PGP SIGNATURE-----
 
T

Tad McClellan

shade said:
I still find myself using <<HERE statements fairly often.
^^^^^^^^^^^^^^^^^

A "here-doc" is not a statement.

It is just another way of quoting strings, like '', q//, "" and qq//.
 
S

shade

Eric J. Roode said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



No, this is not what you're doing. This is full of syntax errors! In
the future, please copy/paste the code that you are referring to, rather
than re-typing it. It will save you time and effort and will help others
to help you better.

That said, the answer to your question is "it's a matter of style." I
personally have done quite a lot of CGI programming, and while I find the
CGI module absolutely indispensible, I prefer to generate my own HTML,
either with a templating mechanism or by using strings and print
statements. I personally feel that CGI.pm's html-generating operators
don't buy you a whole lot, and in some cases make things harder. I would
write a loop much like you have done above. Well, I would have used map,
but that again is a matter of taste.

However, as I said this is a matter of style. *Many* others feel
differently, and wouldn't write CGI programs without CGI.pm's HTML
generating functions/methods. Try both styles, decide which you prefer.

- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPy8UVWPeouIeTNHoEQKmHQCcCfMgML5n+tYm53TauQbB7ITLHhQAoObR
l4tQbmaonm3IlfO/7CEP4qR8
=tmhv
-----END PGP SIGNATURE-----

Ack. Yes, the code is full of errors; sorry about that. I had the copy
only accessible via modem, so decided to try and retype it from memory. My
memory is fuzzy at night; actually, it is always fuzzy. Next time, I'll cut
and paste.

It never dawned on me that <<HERE was just quoting. I was initially using
"print qq'" but found the other to be more readable. As for map, I'll be
honest, I'm not sure what that is yet. But I'm going to find out right now!

Thanks for the suggestions.
shade
 
T

Tad McClellan

Eric J. Roode said:
Do learn about map; don't worry if you don't "get" it right away.


.... because you can always rewrite a map() or grep() as a foreach().
 
W

W K

Tad McClellan said:
... because you can always rewrite a map() or grep() as a foreach().

Is there a particularly good reason for using "map()" ?

I always think that foreach has more scope to add extras in the block, and
is less confusing to the poor old C programmers who sometimes look at my
code.
 
S

shade

Eric J. Roode said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Is there a particularly good reason for using "map()" ?

I always think that foreach has more scope to add extras in the block,
and is less confusing to the poor old C programmers who sometimes look
at my code.

Aw, to heck with them if they can't learn The Perl Way. ;-)

map() is nice because you can use it in expressions. It can make many
things shorter and nicer.

Instead of:
@lines = <FILE>;
foreach (@lines) {
chomp;
$_ = uc;
}

you can do:
@lines = map {chomp; uc} <FILE>;

And of course, you can chain things together with map, as in the famous
Schwartzian transformation:

@sort = map $_->[0], sort {$a->[1] cmp $b->[1]} map [$_, f($_)], @in;

As Tad pointed out, this can be done with foreach(), but it's uglier.

- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPzBBs2PeouIeTNHoEQJ9cQCg20tXexBpqi1X83LvcmqVKtT2i5gAniN0
eA5xjIxQ/LAQ8nQWL6ekQI72
=vOPL
-----END PGP SIGNATURE-----

Aha!

Now that example cleared it up for me. Thanks. I love this language!

shade
 
W

W K

Aw, to heck with them if they can't learn The Perl Way. ;-)

map() is nice because you can use it in expressions. It can make many
things shorter and nicer.

Instead of:
@lines = <FILE>;
foreach (@lines) {
chomp;
$_ = uc;
}

you can do:
@lines = map {chomp; uc} <FILE>;

Also, we used to have a manager type who used to ask how many lines of code
we had written today.
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Also, we used to have a manager type who used to ask how many lines of
code we had written today.

Ah, let's hear it for useless code metrics! :)

Well, in that case, you could write

@lines
=
map
{
chomp;
uc;
}
<FILE>;

Or maybe this would have made the manager happy:

my @rawlines;
my @lines;
while (<FILE>)
{
push @rawlines, $_;
}
my $rl;
foreach $rl (@rawlines)
{
my $line = $rl;
chomp $line;
$line = uc $line;
push @lines, $line;
}

- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPzIlpGPeouIeTNHoEQK2mACg3pKvncEFEbvrBkA2KNAn32uym1gAoLUV
trp8cLXNirHqUQu9i9jTE5ly
=PV6h
-----END PGP SIGNATURE-----
 

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
474,121
Messages
2,570,713
Members
47,282
Latest member
hopkins1988

Latest Threads

Top