in reply to I need outputting the results of a query in a formatted way to a scalar, so that I can email the results
Or do I just need to fix the syntax.
That will be needed, yes. You should move the open, close and format statements outside the loop. You should ensure that you have alternate format strings with values. Here's an SSCCE to get you started using a subset of the form:
#!/usr/bin/env perl use strict; use warnings; my $report = print_query_results (); print "Here comes my report: \n$report\n"; exit; sub print_query_results { my @foo = ([9, 8, 7], [6, 5, 4]); my $blurb = 'Whatever'; my $report; my ($sql_id, $min_last_load_time, $buffer_gets); format REPORT = @|||||||||||||||@||||||||||||||||||||@||||||||||||||| 'SQL_ID', 'min_last_load_time','buffer_gets' @|||||||||||||||@||||||||||||||||||||@||||||||||||||| $sql_id, $min_last_load_time, $buffer_gets @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $blurb . open REPORT, '>', \$report; for my $aref (@foo) { ($sql_id, $min_last_load_time, $buffer_gets) = @$aref; write REPORT; } close REPORT; return $report; }
There are more modern ways of achieving the same end. While format is useful for the odd quick-and-dirty script here and there I wouldn't recommend spending large amounts of time or effort getting into the details. If you need this sort of thing often, consider something like Template as a better investment.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: I need outputting the results of a query in a formatted way to a scalar, so that I can email the results
by gandolf989 (Scribe) on Sep 20, 2018 at 20:05 UTC | |
by poj (Abbot) on Sep 20, 2018 at 21:12 UTC | |
by gandolf989 (Scribe) on Sep 25, 2018 at 18:42 UTC | |
by hippo (Archbishop) on Sep 20, 2018 at 21:15 UTC | |
by BillKSmith (Monsignor) on Sep 20, 2018 at 20:48 UTC |