Tanalis has asked for the wisdom of the Perl Monks concerning the following question:
I'm currently working on a large project, part of which involves converting a large number of reports to use database data and formats.
I'm getting the data from the DB happily enough, and have defined a set of formats which I've put into a module of their own to try and keep the code readable and maintainable (the current reporting scripts are neither).
The script compiles happily with the format module included, but when it comes to outputting the data I simply get the format itself and pages full of blank lines, as if the write is happening but there's no data in the variables.
I've consulted the Camel (2nd Ed.), which says that variables must be in scope when the format is defined in order to be used in that format. I'm ensuring this is happening (I think) by having each set of formats in a subroutine (in the module), which I call just prior to the first write to carry out the format definition.
I'm guessing that this has something to do with variable scope, but I can't seem to find a way to solve the problem or work round it.
If anyone has any comments or ideas, they'd be welcomed. Example code is below.
# module Formats.pm use strict; package Formats; sub admin_usage_reports { format admin_usage_top = A D M I N U S A G E Date: @<<<<<<<<<< $report_date Username Login Logout Activity ---------------------------------------------------------- . format admin_usage_data = @<<<<<<< @<<<<<<< @<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<< $usr, $login, $logout, $activity . } # main script Formats::admin_usage_reports; $^ = "Formats::admin_usage_top"; $~ = "Formats::admin_usage_data"; write;
--Foxcub
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Formats and Variable Scope
by jreades (Friar) on Nov 19, 2002 at 12:07 UTC | |
by Tanalis (Curate) on Nov 19, 2002 at 12:29 UTC | |
|
Re: Formats and Variable Scope
by jmcnamara (Monsignor) on Nov 19, 2002 at 14:16 UTC | |
|
Re: Formats and Variable Scope
by TheHobbit (Pilgrim) on Nov 19, 2002 at 18:31 UTC | |
by chromatic (Archbishop) on Nov 19, 2002 at 21:11 UTC |