View Full Version : Help printing HTML in Perl
angel_warrior_3
07-12-2004, 09:15 AM
I am new to perl (progging for about 1/4 a year now) and have stuck to simple code thus far, however, simple code has had me printing increasingly complicated and large HTML pages from the actual program file, and this has obviously resulted in horridly messy code.
So, I decided to step into unknown territories and have so far failed to solve this problem.
That is to say, what I'm trying to do is leave the references on the file then just print the file through the program, therefore slashing the program size and making the code much neater.
However, so far it has not recognised the variable references in the file. (I guess this is a simple problem, but hey)
here's a snapshot of what I'm doing:
HTML FILE: (created with dreamweaver)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
hello $nick
</body>
</html>
__________________
Prog: (created with Komodo)
#!/perl/bin -w
print "Content-type: text/html", "\n";
print "Pragma: no-cache", "\n\n";
$nick = "pumpingpump";
open (NICKTEST, "nicktest.htm");
$count = 0;
while ($nicktest = <NICKTEST>) {
$nickstore[$count] = $nicktest;
$count += 1;
}
close (NICKTEST);
print (@nickstore);
__________________
the output is:
Content-type: text/html
Pragma: no-cache
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
hello $nick
</body>
</html>
and of course, if you have read the above, I want that $nick in the output to be "pumpingpump"
can anyone help me?
MattJakel
07-12-2004, 09:30 PM
The problem is that when you read that file, it reads it as \$nick not $nick, which returns the character $ followed by nick instead of the variable. As far as I know, there's no easy way to replace the variables already stored in an outside file with a module or anything. You can do this though (insert as the first line in the while loop):
$nicktest =~ s/\$nick/$nick/gis;
That's using a regular expression to replace the variable. You can do this to replace all variables:
$nicktest =~ s/\$(.*?) /${$1}/gis; # the space is important
Hope this helps!
Matt
angel_warrior_3
07-13-2004, 01:26 AM
cool, I hadn't thought that it would have read as "\$nick", that was all I needed, thanx, you're explanation was great and your even saved me the time writing the code :P.
thanx again MattJakel
dswimboy
07-13-2004, 11:03 PM
why not use Server Side Includes???
http://hoohoo.ncsa.uiuc.edu/docs/tutorials/includes.html
angel_warrior_3
07-14-2004, 04:20 AM
nah, I don't think so, SSI isn't what I'm looking for.
Well, ahoyhoy, there be a problem avast - in the name of array variables.
let's have a peek at the HTML, shall we?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2">$nick[1] </font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2">$nick[1] </font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2">$nick[1] </font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2">$nick[1] </font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2">$nick[1] </font></font></font></div></td>
</body>
</html>
now, I just threw in a chunk of a table, who cares if the HTML is invalid, lets focus on the output of the program when passing this though it (and if you've noticed, I put a space between the variable and the table markers, this is the only way I was able to get it to work, so that's something I'd like some help with as well);
#!/perl/bin -w
print "Content-type: text/html", "\n";
print "Pragma: no-cache", "\n\n";
$nick[1] = "pumpingpump";
open (NICKTEST, "nicktest.htm");
$count = 0;
while ($nicktest = <NICKTEST>) {
$nickstore[$count] = $nicktest;
$nickstore[$count] =~ s/\$(.*?) /$$1/gis; <-------------- if you're wondering why I have all of this, don't worry, because it works perfectly and covers most possibilities.
$nickstore[$count] =~ s/\@(.*?) /@$1/gis;
$nickstore[$count] =~ s/\$(.*?)$/$$1/gis;
$nickstore[$count] =~ s/\@(.*?)$/@$1/gis;
$matches[$count] = $1;
$count += 1;
}
close (NICKTEST);
print ("@nickstore\n");
$counter = @matches;
$count2 = 0;
while ($counter >= 0) {
print ("$count2. $matches[$count2]\n");
$count2 +=1;
$counter -=1;
}
die ("\n");
ok, now the output, the last part if to keep track of what's stored in variable $1. let's have a look at it (the numbered part at the end is the output I discussed earlier in this very paragraph).
Content-type: text/html
Pragma: no-cache
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2"></font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2"></font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2"></font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2"></font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2"></font></font></font></div></td>
</body>
</html>
0.
1.
2.
3.
4.
5.
6.
7.
8. nick[1]
9. nick[1]
10. nick[1]
11. nick[1]
12. nick[1]
13. nick[1]
14. nick[1]
15.
ok, taking a look at the chunk of a table, you can see:
" <td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2"></font></font></font></div></td>"
namley, $nick[1] was recognised but the value of it (pumpingpump) was not placed in it's stead. Oddly enough, my proggie works fine for pretty much everything but this (ie. not specifying a value in an array, but placing it as @name for example works fine)
Sadly, when printing out massive tabulated pages, I can't forgo the humble array variable, so I want to get around this problem as soon as possible.
MattJakel
07-14-2004, 05:21 AM
Well, now I'm stumped. I'm 99% sure it's because the brackets are \[ and \] instead of [ and ], but I don't know how to use a regular expression to switch these because [] without backslashes in a regular expression has its own meaning... I will think about this and try a few things, but does anyone else have any ideas?
Matt
angel_warrior_3
07-14-2004, 08:35 AM
yes, and hence my dilemma. the odd thing is that it actually recognized the term. however, when it replaced the term, command follow didn't work in my favor - since it would then replace it as $nick, but the [1] would be disregarded, and since there's no $nick variable in the prog (just @nick), it did just that, replaced it with nothing because nothing is the value of $nick. Hilarious.
at least, that's what I thought and therefore thought you agreed with me upon.
maybe there's a comand other from open and read that actually recognises the references?
has anyone got any idea?
angel_warrior_3
07-18-2004, 01:08 AM
so no one has an idea?
MattJakel
07-19-2004, 03:58 AM
I got it! By simply including curly brackets around the name of the array I let perl know that it was dealing with an array and not a character class, solving the problem.
Here's the code I came up with:
#!/usr/bin/perl
print "Content-type: text/html", "\n";
print "Pragma: no-cache", "\n\n";
$nick[1] = "pumpingpump";
open (NICKTEST, "test.txt");
$count = 0;
while ($nicktest = <NICKTEST>) {
$nickstore[$count] = $nicktest;
if ($nickstore[$count] =~ m/\$(.*?)\[(.*?)\] \n/gis) {
&arrfix;
} else {
$nickstore[$count] =~ s/\$(.*?) /$${1}/gis;
$nickstore[$count] =~ s/\[/[/gis;
$nickstore[$count] =~ s/\]/]/gis;
$matches[$count] = $1;
}
$count += 1;
}
close (NICKTEST);
print ("@nickstore\n");
$counter = @matches;
$count2 = 0;
while ($counter >= 0) {
print ("$count2. $matches[$count2]\n");
$count2 +=1;
$counter -=1;
}
die ("\n");
sub arrfix {
$nickstore[$count] =~ s/\$(.*?)\[(.*?)\]/${$1}[$2]/gis;
$matches[$count] = "$1\[$2\]";
}
:thumbsup: Matt
angel_warrior_3
07-20-2004, 09:48 AM
I don't know, didn't work out for me.
my code (pretty much your code with exit(0) at the end instead of die and a different perl directory, but yes)
#!/perl/bin -w
print "Content-type: text/html", "\n";
print "Pragma: no-cache", "\n\n";
$nick[1] = "pumpingpump";
open (NICKTEST, "nicktest.txt");
$count = 0;
while ($nicktest = <NICKTEST>) {
$nickstore[$count] = $nicktest;
if ($nickstore[$count] =~ m/\$(.*?)\[(.*?)\] \n/gis) {
&arrfix;
} else {
$nickstore[$count] =~ s/\$(.*?) /$${1}/gis;
$nickstore[$count] =~ s/\[/[/gis;
$nickstore[$count] =~ s/\]/]/gis;
$matches[$count] = $1;
}
$count += 1;
}
close (NICKTEST);
print ("@nickstore\n");
$counter = @matches;
$count2 = 0;
while ($counter >= 0) {
print ("$count2. $matches[$count2]\n");
$count2 +=1;
$counter -=1;
}
sub arrfix {
$nickstore[$count] =~ s/\$(.*?)\[(.*?)\]/${$1}[$2]/gis;
$matches[$count] = "$1\[$2\]";
}
exit(0);
the file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2">$nick[1] </font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2">$nick[1] </font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2">$nick[1] </font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2">$nick[1] </font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2">$nick[1] </font></font></font></div></td>
</body>
</html>
the output
Content-type: text/html
Pragma: no-cache
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2"></font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2"></font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2"></font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2"></font></font></font></div></td>
<td><div align="center"><font size="2"><font color="#FFFFFF"><font size="2"></font></font></font></div></td>
</body>
</html>
0.
1.
2.
3.
4.
5.
6.
7.
8. nick[1]
9. nick[1]
10. nick[1]
11. nick[1]
12. nick[1]
13.
14.
15.
angel_warrior_3
07-21-2004, 08:48 AM
fantastic, it's been solved by a person in another forum (thank you so much atlas)
his post, it's self-explanitory
"Your code is doing essentially $$"var[0]", which won't work because there is no scalar named "var[0]" only an array "var" that takes an offset. To demonstrate:
@test = ("0","1","2");
$val_a = "test";
$val_b = "test[0]";
print "first try: " . $$val_a[0] . "\n";
print "second try: " . $$val_b . "\n";
Results:
first try: 0
second try:
Anyhow, here's the code:
$nickstore[$count] = $nicktest;
$nickstore[$count] =~ s/\$(\w+?)\[(\d+)?\]/$$1[$2]/gis;
$nickstore[$count] =~ s/\$(\w+?)(\W)/$$1$2/gis;
$nickstore[$count] =~ s/\@(\w+?)(\W)/@$1$2/gis;
$matches[$count] = $1;
I've cleaned up the regexes and made it so you don't need to include a space after the variables."
yay! this shall be so useful
MattJakel
07-22-2004, 06:50 AM
I'm glad you solved your problem!
Matt
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.