While I'm still pondering about how to best solve the above question by the
various solutions posted in response, I have now a different question,
although ultimately for the one and same purpose.
The below script is a bare-bone version, without the language multi-arrays
attempt. It simply generates groups of unordered html-lists which form the
basis of a navigation system.
The navigation will have a bunch of UL's and LI's glued together in
css drop menu fashion, but I've cut that css out from this post.
Each group of links will have several LI's nested within the first-level UL
and LI entry, like this:
<ul>
<li class=yellow>page one
<ul>
<li>DING</li>
<li>Something</li>
</ul>
The current page is identified through an $ENV{"DOCUMENT_NAME"} call. The
whole thing is generated via SSI and the script resides in a dot-pl file.
In case the current page exists in one of the hash arrays, the resulting
css-class is set to "yellow" and subsequently the background of the
list items contained in its css-block all become yellow. For example, the
background of the first <li> and nested <ul><li>'s will be yellow when the
page DING is viewed.
For user-friendliness sake I'd like the current page (e.g. DING) not to be
a link to itself when it is viewed.
My question is therefore, how would one normally omit the enclosing <a
href=$_> and </a> html-code output based on a particular condition, in this
case if and where the current page name ($current:
age) match exists?
for (\%activities) {
my $firstkey = each %$_;
print "<li class=$class::value><a
href=$firstkey>$activities{$firstkey}</a>\n";
print "<ul>\n";
print "<li><a href=$_>$activities{$_}</a></li> \n" while local $_ = each
%$_;
print "</ul>\n";
print "</li>\n";
}
The complete script is included below:
#!/usr/bin/perl -w
print "Content-type: text/html\n\n";
# bg css-color to visually indicate which group of
# links/ul current page exists in
######## css bits #########
print "<head>\n";
print "<style>\n";
print ".green {background:green;}\n";
print ".yellow {background:yellow;\n";
print "</style>\n";
print "</head>\n\n";
######## some pre-procedures ########
use Tie::IxHash;
use strict;
use warnings;
tie my %location, "Tie::IxHash";
tie my %activities, "Tie::IxHash";
# Place CGI environment variable in
# $current:
age global
# needed later to identify if page is in group
if ($ENV{"DOCUMENT_NAME"}){
$current:
age = $ENV{"DOCUMENT_NAME"};
}
else {
$current:
age = "undefined";
}
print $current:
age;
######### the hashes with links ##########
%location = ('page1.html' => 'page one',
'ding.html' => 'DING',
'something.html' => 'Something');
%activities = ('fishing.html' => 'Fishing',
'diving.html' => 'Diving',
'drinking.html', => 'Beer');
##### display first group of links with a loop ####
if (exists($location{$current:
age})){
$class::value = "yellow";
}
else {
$class::value = "green";
}
print "<ul>\n";
for (\%location) {
my $firstkey = each %$_;
print "<li class=$class::value><a
href=$firstkey>$location{$firstkey}</a>\n";
print "<ul>\n";
print "<li><a href=$_>$location{$_}</a></li> \n" while local $_ = each %$_;
print "</ul>\n";
print "</li>\n";
}
print "</ul>";
#### display second group of links with loop ####
if (exists($activities{$current:
age})){
$class::value = "yellow";
}
else {
$class::value = "green";
}
print "<ul>";
for (\%activities) {
my $firstkey = each %$_;
print "<li class=$class::value><a
href=$firstkey>$activities{$firstkey}</a>\n";
print "<ul>\n";
print "<li><a href=$_>$activities{$_}</a></li> \n" while local $_ = each
%$_;
print "</ul>\n";
print "</li>\n";
}
print "</ul>";
# etc...
No doubt there exists more compact methods for the whole procedure,
especially when there are many more groups of links. But code-bloat is not
a problem here, as the script simply generate static pages once in a while
which are later sent up on a server via automated ftp procedures. Instead
configurability and exception to rules may be more important in the future.
The purpose of the perl script is simply to remove tedious and error prone
cross page html coding, while still retaining that old tailor made quality.