Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML output from a string

Status
Not open for further replies.

chrismassey

Programmer
Joined
Aug 24, 2007
Messages
264
Location
GB
I have a string which contains part of some HTML code, however it contains a scalar variable. If I don't wrap the HTML code in a string I recieve an Error 500. But because its a string, it doesn't print the value of the scalar, but instead print the scalar itself. Here is one of my strings...
Code:
<tr>
         <td width="20%" align="center"><font face="Arial" size="2" color="#008080"><b>$one</b></font></td>
         <td width="20%" align="center"><font size="2" face="Arial">Interview Techniques</font></td>
         <td width="20%" align="center"><font size="2" face="Arial">FY 1,2 <font color="#008080">|</font> ST 1,2,3,4 <font color="#008080">|</font> SPR <font color="#008080">|</font> STG <font color="#008080">|</font> GPT</font></td>
         <td width="20%" align="center"><font color="#336600"><img border="0" src="../TriBulletGre.bmp" width="14" height="14"></font><font size="2" face="Arial">
         <font color="#336600">Available</font>
         <br>
         Cost: £169.99
         </font></td>
         </tr>'

As you can see there is a $one which has a value of 99999

But there printed output isn't "99999". its "$one".

So, how do I go about printing the value of $one instead of "$one"

Thanks, Chris
 
I forgot to mention that the code above is attached to another scalar

so...

$htmlrow = 'HTML CODE ABOVE';
 
Ok, I think I found the answer

Istead of wrapping the HTML code in strings, i've ised interpolation (qq). I guess I could have also backslashed special chracters but I didn't try.

So it now looks like this...

$htmlrow = qq(HTML CODE ABOVE);
 
I gather you are reading the HTML from an external file.
A simple way would be to replace the string '$one' with a word, I have used MARKER but any text would do, and then do a substitution.
Code:
$htmlrow=~ s/MARKER/$ActualValue/;
There is probably a much neater way of doing it.


Keith
 
Scalars defined within single quotes won't interpolate variables, so when you type $one, you literally get a '$' character followed by 'one'. To interpolate variables you need to use double quotes, but this would mean you have to escape all the double quotes in your HTML with backslashes. Instead, you can use one of the other quoting methods to act in the same way. Just make sure the delimiter you use doesn't appear in your HTML or it'll break your script. Example:
Code:
my $htmlrow = qq!<tr>
         <td width="20%" align="center"><font face="Arial" size="2" color="#008080"><b>$one</b></font></td>
         <td width="20%" align="center"><font size="2" face="Arial">Interview Techniques</font></td>
         <td width="20%" align="center"><font size="2" face="Arial">FY 1,2 <font color="#008080">|</font> ST 1,2,3,4 <font color="#008080">|</font> SPR <font color="#008080">|</font> STG <font color="#008080">|</font> GPT</font></td>
         <td width="20%" align="center"><font color="#336600"><img border="0" src="../TriBulletGre.bmp" width="14" height="14"></font><font size="2" face="Arial">
         <font color="#336600">Available</font>
         <br>
         Cost: £169.99
         </font></td>
         </tr>!;
 
Thank you ishnid. I resolved the issue in the exact same way, but took me ages to find out how. Should have just waited for your response hehe. Thanks lots
 
Hey audiopro, only just noticed your reply. You are correct in what I am doing. Your method seems very tidy, so I think I will try it because at the moment my complete code is in a mess :s. Thanks
 
You can expand variables in single-quotes strings or in input from a text file which is treated like a single-quoted string.


But since this is the same as using eval some caution is probably in order. You don't want to write the regexp in a away that will run arbitrary user input as code. I am unsure if there is any advantage using this method over the one audiopro posted.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top