Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

while calling my one of scripts, which searches a database and throws the informataion on website in html form, for one of the field i want to provide a link like:
<tr> <td width="174" height="36"><b><font color="#0000FF" size="2" fa +ce="Century Schoolbook">Author's Email Address:</font></b></td> <td width="408" height="36">><font face="Times New Roman" size=" +2"><a href="mailto:$afld[3]"></a></font></td> </tr> <tr>
the link here ( thru a href is not working, )it does not display the email address at all, i want it to be displayed in the form of html link can some body tell me how to do this ? rt

Replies are listed 'Best First'.
Re: how to put perl variables inside html tags
by tachyon (Chancellor) on Jul 28, 2001 at 03:24 UTC

    This is how you might do it from a perl script using a here page. Herepages are generally preferable to masses of print statements if you are not using some form of templating. I have added the debugging line as nothing will print is $afld[3] is not defined.

    In you example you have unmatched tags, an extra > before you second font tag which can sometimes cause certain browsers to choke. This is not however your main problem. You have no display text defined for the email link - if you view source you will probably find the link is there but there is no text associated with it making it a little hard to see. You need to place some text where *****Some Link***** is in this example. You can use your variable again if you want the link to display as the raw email address.

    print "<h1>Email address not defined!</h1>" unless $afld[3]; print <<END_OF_HTML; <tr> <td width="174" height="36"> <b><font color="#0000FF" size="2" face="Century Schoolbook"> Author's Email Address:</font></b> </td> <td width="408" height="36"> <font face="Times New Roman" size="2"> <a href="mailto:$afld[3]">*****Some Link*****</a></font> </td> </tr> END_OF_HTML
    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: how to put perl variables inside html tags
by princepawn (Parson) on Jul 28, 2001 at 02:40 UTC
    A number of solutions for this are documented in the section "Embedding Perl in HTML" on the Apache website.

    And, what you want to do is commonly known as templating for you can always Take a look at CPAN or if you want a fast solution, try CGI in combination with HTML::Template.

    And there are plenty of nodes relevant to this here, for example this one.

Re: how to put perl variables inside html tags
by dvergin (Monsignor) on Jul 28, 2001 at 02:48 UTC
    You are not clear whether this patch of html is the output from your program or snipped from your Perl script itself. And if it is from your Perl script, it is not clear how you are printing this out:

    Are you using CGI for this?
    Is it in a 'here doc'?
    Are you using a print statement for each line?

    The value $afld[3] can be printed or interpolated just fine. The only thing to be careful of is how to incorporate the double-quotes in your html and still interpolate the array value.

    The problem of course is that if you want to interpolate the value in a string, the string has to be specified in a double-quotish manner. But if you just enclose it in double-quotes, the html double-quotes mess things up. There are a variety of approaches:

    print qq[<a href="mailto:$afld[3]">]; or print '<a href="mailto:' . $afld[3] . '">';
    ...to give just two examples.

    Note also: there is nothing between <a href="mailto:$afld[3]"> and the </a> tag. If this is an accurate snippet, the mailto: link is there but no link text is being displayed because you have specified nothing.

    Update: If you have a managable number of these values to interpolate, this approach works fine. If you are doing more ambitious things with lots of interpolated values and other fancy programmatic stuff, check into the embedding/templating solutions that have been proposed.

      I am outputtin this html page thru ther perl script by using print <<HTML here all html code HTML i tried putting <a href="mailto:$afld[3]">$afld[3]</a> but even it didn't work although when i am removing <a> </a> totally, and just printing $afld[ +3] its printing,but without a link.
        Odd... Somewhere along the line you have done something you are not accounting for. At about this point, I would stick the following into the middle of your 'print <<HTML' block:
        print <<HTML ...stuff that's already there ------BEGIN TEST------- Here is afld3: |$afld[3]| <a href="mailto:someone@somewhere.com">mail link 1</a> <a href="mailto:someone@somewhere.com">$afld[3]</a> <a href="mailto:$afld[3]">mail link 2</a> <a href="mailto:$afld[3]">$afld[3]</a> **What we want** -------END TEST------- ...more stuff that's already there HTML
        Then run it and look at the html code this prints (not just what shows up visible in the browser) to see what is happening.
Re: how to put perl variables inside html tags
by mitd (Curate) on Jul 28, 2001 at 09:35 UTC

    Take a look here for a simple fast a clean approach.

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'