Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Create HTML files using perl on the fly

by sKore
on Nov 20, 2007 at 01:18 UTC ( #651829=sourcecode: print w/replies, xml ) Need Help??
Category: HTML Utility
Author/Contact Info sKore
Description: Create HTML files using perl on the fly.

#!/usr/bin/env perl 
use warnings; 
use strict; 
use Data::Dumper; 

my $file="MySpace.html"; 
my $cmdString = "/tmp/"; 
chdir($cmdString)|| die "Error: could not '$cmdString'"; 
if (-e $file) #if the file exists 
{ 
$cmdString="rm $file"; 
system($cmdString)==0 or die "Error: could not '$cmdString'"; 
} 
open(my $FILE, ">>$file") or die "Cannot open $file: $!"; ## >> means 
+append to the end of file. 
my $fontColor="D2L"; 
my $empNo=43432; 
my $empName="Marconi"; 
my $empDesg="Managed Director"; 
my $empSal="59999.99"; 
print $FILE "<HTML>\n";
print $FILE "<table width=\"100%\">\n"; 
print $FILE "<tr><th colspan=\"4\" class=\"H25H\">HelloWorld</th></tr>
+\n"; 
print $FILE "<tr><th class=\"H25L\">Emp#</th><th class=\"H25L\">Name</
+th><th class=\"H25L\">Designation</th><th class=\"H25L\">Salary</th><
+/tr>\n";

If ( $empDesg == "Managing Director") 
{ 
$fontColor="W30L"; 
} 
print $FILE "<td class=$fontColor>$empNo</td><td class=$fontColor>$emp
+Name</td><td class=$fontColor>$empDesg</td><td class=$fontColor>$empS
+al</td></tr></table>\n";

print $FILE "<br><br>\n"; 
print $FILE "<head><META http-equiv=\"Content-Type\" content=\"text/ht
+ml; charset=UTF-8\"><style type=\"text/css\">table {border: 1 solid b
+lack; border-collapse: collapse; } th {border: 1 solid black; width: 
+20%; text-align: left; background: #B8B8B8} td {border: 1 solid black
+; width: 80%} .H25H {border: 1 solid black; width: 25%; text-align: c
+enter} .H25L {border: 1 solid black; width: 25%; text-align: left} .D
+2L {border: 1 solid black; width: 25%; text-align: left; background: 
+#DDDDDD} .W30L {border: 1 solid black; width: 25%; text-align: left; 
+background: #DDDDDD ; font-size : 12pt; font-weight: bold; color:red}
+</style></head></html>\n";

close($FILE); 

print "Your html has been created";
Replies are listed 'Best First'.
Re: Create HTML files using perl on the fly
by ww (Archbishop) on Nov 20, 2007 at 03:19 UTC
    Not to be cruel, but:
    1. Code does not compile:
      perl -c sKore.pl
      syntax error at sKore.pl line 26, near ")
      {"
      sKore.pl had compilation errors.
    2. test in 26 needs to be "eq" and not "=="
    3. HTML is non-compliant
      • No <body> and </body> tags (required)
      • </html> tag appears before the intended bodycontent
      • Unquoted attributes (W30L for example)
      • Lacks a <!DOCTYPE ...and much more

    But really more-to-the-point: what does this one-off code do to make life easier? AFAICS, nada.

    By contrast, HTML:Template, MASON, and friends may actually be helpful to you. They'll certainly be worth the time you spend studying them... though perhaps only after you master html.

    Update Formating, changed from <c>...</c> to <blockquote... around compilation error, etc.
Re: Create HTML files using perl on the fly
by hossman (Prior) on Nov 20, 2007 at 17:43 UTC

    Ditto everything ww said. in addition...

    • when posting Code Contributions please use a more descriptive description then just cutting and pasting the title of your post ... what does your code do? why might people want to use it? how would people go about using it if they wanted to?
    • to be frank: your code doesn't seem that useful beyond a single use case -- the only input it takes in is a directory name to write a file whose name is hardcoded, and all of the data that will be written to that file is hardcoded ... the mount of time necessary for someone to customize your script to be useful for them seems like it would be approximately the same amount of time as if they wrote their own script.
    • your first few error messages don't distinguish themselves ... they give no indication of what the problem was with the file.
    • you don't need to use "system('rm')" to delete a file, that's what unlink is for.
      There are also, as it happens, systems on which there is no rm. unlink travels better.
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://651829]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2020-09-20 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (122 votes). Check out past polls.

    Notices?