I'd like to streamline an already simple CGI script using foreach or while and a hash of URL's.

The example below shows what works, as well as how it *doesn't* work when placed inside a CGI.pm HTML table.   I've read TPJ's Coping with Scoping plus perldoc CGI.pm, but am just not getting it.

Direction or sample code greatly appreciated.
    cheers,
    ybiC

#!/usr/bin/perl -wT use strict; use CGI::Carp qw (fatalsToBrowser); use CGI::Pretty qw(:all); my %urlhash = ('Perl Monks' => "http://www.perlmonks.org/", 'Debian' => "http://www.debian.org/", 'OpenBSD' => "http://openbsd.org/", ); print header(-type => 'text/html', -expires => 'now'), start_html (-title => 'hash.cgi'); # FOREACH & WHILE SYNTAX WORKS FINE HERE #print "<p> ==HASH FOREACH=="; #foreach my $key (keys %urlhash) { # print "<br>", a({href => "$urlhash{$key}"}, "$key"), # } #print "<p> ==HASH WHILE=="; #while((my $key, my $value) = each(%urlhash)) { # print "<br>", a({href => "$urlhash{$key}"}, "$key"), # } print table, (Tr, (td, ( # BUT NEITHER WORKS FROM INSIDE CGI.PM TABLE. # 'syntax error at line 29, near "foreach"' foreach $main::key (keys %urlhash) { print "<br>", a({href => "$urlhash{$key}"}, "$key"), } # 'syntax error at line 34, near "while"' while(($main::key, $main::value) = each(%urlhash)) { print "<br>", a({href => "$urlhash{$key}"}, "$key"), } ), ), ); # close td, Tr, table print end_html;

In reply to while, foreach inside CGI.pm table by ybiC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.