Hello, I'm fairly new to perl, and am trying to learn it by developing an application for my own use.
I am basically trying to write out html pages of my music collection. I read in from an Access database, and then write out the html, each artist to their own page, and creating sections for each album within the artist's page, and then a main index.html .
My question is, the code works. True, it may be baby perl, but fact is it does what I want it to do, except I am having a problem with the Open and Close commands. The html is written, but I get "print() on closed filehandle ARTISTOUT at C:\PROGRA~1\APACHE~1\APACHE\CGI-BIN\ex
port.pl line 96.", which is this line:
"print "<tr><td>$row[1] - $row[2] - $row[0]</td></tr>";". I am dealing with roughly ~14000 entries here, and I think that the problem may be I am trying to write out again before the previous process is finished. It works on smaller groups of entries (a few hundred) with no problems.
#! d:/perl/bin/perl -w
use DBI;
use File::Basename;
use strict;
open MASTEROUT, ">index.html";
select MASTEROUT;
my $DSN = 'driver=Microsoft Access Driver (*.mdb);dbq=Media.mdb';
my $dbh = DBI->connect("dbi:ODBC:$DSN", '','') or die "$DBI::errstr\n"
+;
my $artistHolder;
my $albumHolder;
my $query = $dbh->prepare("SELECT title, artist, album FROM tblMedia O
+RDER BY artist, album");
$query->execute();
my @row = $query->fetchrow_array;
$artistHolder = $row[1];
$albumHolder = $row[2];
print "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'
+'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
print MASTEROUT "<html><head><title>Overall</title></head><body><table
+ summary='Overall' width='640' align='center' border='1'><tr><td><tab
+le summary='Artist Listing' width='100%'>";
print MASTEROUT "<tr><td><a href='$row[1].html'>$row[1]</a></td></tr>"
+;
open ARTISTOUT, ">".$row[1].".html";
select ARTISTOUT;
#Overall table
print "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'
+'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
print "<html lang='en-US'><head><title>$row[1]</title>";
print "<link rel='stylesheet' type='text/css' href='style.css'></link>
+</head><body>";
print "<table summary='Overall' border='1' width='640' align='center'>
+<tr><td>";
#per artist
print "<table summary='Artist' width='100%' border='0'>";
#per album
print "<tr><td colspan='2'><b>$row[1]</b></td></tr>";
print "<tr>";
print "<td width='5%'> </td>";
print "<td><table summary='Album' width='100%' align='center' border='
+0'>";
print "<tr><td class='tableTitle'>$row[1]</td></tr>";
while ( @row ) {
#Control break on Album
# if (@row[2] ne $albumHolder && @row[1] ne $artistHolder)
if ($row[2] ne $albumHolder && $row[1] eq $artistHolder)
{
print "</table></td></tr>";
$albumHolder = $row[2];
print "<tr><td> </td><td><table summary='Album' width='10
+0%'><tr><td><br /><br /><i>$row[1] - $row[2]</i></td></tr>";
print "<tr><td><hr /></td></tr>";
}
#Control break on Artist
if ($row[1] ne $artistHolder)
{
#End previous artist's listings (End album, then artist print "
+</table></td></tr></table></td></tr></table></body></html>";
close ARTISTOUT;
open ARTISTOUT, ">".$row[1].".html";
select ARTISTOUT;
print MASTEROUT "<tr><td><a href='$row[1].html'>$row[1]</a></t
+d></tr>";
#Begin new
print "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transition
+al//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
+
print "<html lang='en-US'><head><title>$row[1]</title>";
print "<link rel='stylesheet' type='text/css' href='style.css'
+></link>";
print "</head><body><table summary='' width='640' align='cente
+r'><tr><td><table summary='Artist' width='100%' border='0'>";
print "<tr><td colspan='2'><hr /></td></tr>";
print "<tr><td colspan='2' class='tableTitle'><b>$row[1]</b></
+td></tr>";
print "<tr><td colspan='2'><hr /></td></tr>";
$artistHolder = $row[1];
$albumHolder = $row[2];
#start out next album
print "<tr><td width='5%'> </td><td><table summary='Album
+' width='100%' border='0'>";
print "<tr><td colspan='2'><a href='index.html'>Back to home</
+a><br /><br /></td></tr>";
print "<tr><td colspan='2'><i>$row[1] - $row[2]</i></td></tr>"
+;
print "<tr><td><hr /></td></tr>";
}
#Print out each entry
print "<tr><td>$row[1] - $row[2] - $row[0]</td></tr>";
#Get the next entry.
@row = $query->fetchrow_array;
}
print "</td></tr></table></html>";
print MASTEROUT "</table></td></tr></table></body></html>";
$dbh->disconnect();
I ask for a little guidance. Can I throttle this to wait for the previous process? Please don't fry me too badly..
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.