Andrew Gaffney wrote: > R. Joseph Newton wrote: >
> I am writing a module that contains functions that I commonly use in my scripts. I > have > written a lot of scripts that generate HTML reports from the data in the MySQL DB. > My boss > wants these reports to spit out their data either in HTML or Excel. I had the idea of > writing functions that take a series of scalars and arrays that describe the data to > be in > the report. Are you sure? In what way? Your response left the entire body of my post intact, so that doesn't narrow down the points of perceived conflict. My perception is that we have all been reinforcing each other's point that prototypes are probably not a good investment of your coding energy right now. Can you be more specific? > > > The functions (generate_report_html() and generate_report_excel()) actually process > the > arrays and such and generate the reports in HTML or Excel. This makes it easier to > create > new reports and greatly cuts down on code duplication. Here are my functions (keep > in mind > I only started writing them at 1AM and they aren't near finished): > > <code> > package Skyline; > > <big SNIP of other module code> > > sub generate_report_html([EMAIL PROTECTED]@) { > my ($title, $columns, $data) = @_; $data is prototyped as an array/list, but you receive it here as a scalar. If you must use the damned prototype, it should be: sub generate_report_html([EMAIL PROTECTED]@) or your p[arameter get should be: my ($title, $columns, @data) = @_; > > print <<' EOF'; > <html> > <body> > <center><img src='/original_logo_60pc.jpg'><p> > <h2>$title Report</h2></center> > <p> > <table width=100%> > <tr> > EOF > foreach (@$columns) { > print "<td><b><u>$_</u></b></td>"; > } > print "</tr>\n"; > foreach my $row (@$data) { > print "<tr>"; > foreach (@$row) { > print "<td>$_</td>"; > } > print "</tr>\n"; > } > print <<' EOF'; > </table> > </body> > </html> > EOF > } > > sub generate_report_excel([EMAIL PROTECTED]@) { Same issue as with the above. It may work, but only by accident. > > my ($title, $columns, $data) = @_; > # No other code written for this function yet > } > </code> > > A typical script that generates a report using these functions would look like: > > <code> > use Skyline; > use CGI; > > my $cgi = new CGI; > my @data; > my $dbh = init_db(); # Module function to connect to MySQL DB using DBI > my $sth = $dbh->prepare("SELECT fname, lname, homephone FROM people ORDER BY lname"); > $sth->execute; > my $ref; > while($ref = $sth->fetchrow_hashref) { > push @data, ["$ref->{lname}, $ref->{fname}", "$ref->{homephone}"]; > } > > my @columns = ("Name", "Phone Number"); > generate_report_html("Customers", @columns, @data); > </code> > > This then generates a nice formatted HTML report with minimal code. What would you > change > (if anything) about the above code (module or report scripts)? > > -- > Andrew Gaffney > Network Administrator > Skyline Aeronautics, LLC. > 636-357-1548 I'd get rid of the prototypes. I would then offer only references as function arguments, unless there is a specific reason why you want the function to deal with only the list of values contained by the array, rather than the array itself. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>