user2630404
user2630404

Reputation: 21

Perl variable inside a HREF

Is it possible to have a variable inside a href HTML link? In other words, could I dynamically generate a series of html links by iterating through an array?

For example, like in this Perl CGI extract:

for ($i = 1; $i <= 10; $i++) {
value=array
<A href="prog1.cgi?data=${value}">$value</A>
}

I am using a different query string value in the link each iteration of the loop. If this is not possible, I would appreciate any other advice.

Upvotes: 2

Views: 1805

Answers (2)

ikegami
ikegami

Reputation: 385897

The HTML is to be printed, so we're simply talking about concatenation. Don't forget to convert your text to the proper format first, though.

use CGI         qw( escapeHTML );
use URI::Escape qw( uri_escape );

for my $value (@array) {
   my $uri = 'prog1.cgi?data='.uri_escape($value);
   my $html = escapeHTML($value);
   print qq{<a href="$uri">$html</a>};
}

or

use CGI         qw( escapeHTML );
use URI::Escape qw( uri_escape );

for my $value (@array) {
   printf '<a href="prog1.cgi?data=%s">%s</a>',
      uri_escape($value),
      escapeHTML($value);
}

Upvotes: 4

RobEarl
RobEarl

Reputation: 7912

Just use the value within a print:

for my $value (@array) {
    printf '<A href="prog1.cgi?data=%s">%s</A>', $value, $value;
}

Upvotes: 1

Related Questions