Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Modules of Formats

by Tanalis (Curate)
on Oct 01, 2002 at 08:54 UTC ( #201949=perlquestion: print w/replies, xml ) Need Help??
Tanalis has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm writing a script that will be generating a large number of similar reports, each of which has its own format header and body.

I'd like to export all of these formats to a dedicated ReportsFormats module, simply to make the script easier to follow and look less cluttered.

I'm after advice and opinions, I guess - is it valid to have a module of formats, how would I go about exporting them (Exporter in the usual way?) and how could I go about avioding the problem of the Perl compiler not finding the variables in the formats because they're not local to the format module itself (though I suppose not using strict is an option there).

Any advice/suggestions would be appreciated. This isn't fundamental .. but I hate cluttered code and for the sheer number of formats we use doing it this way would make it much, much easier to understand and maintain the script.

Thanks in advance
-- Foxcub

Replies are listed 'Best First'.
Re: Modules of Formats
by robartes (Priest) on Oct 01, 2002 at 09:55 UTC
    Hi Foxcub,

    Section 3.2.41 of the Camel Book seems to imply that you can put formats in modules just like any other thingy. Formats live in their own namespace and can be qualified with package names, so that answers your first question.

    As to variable scoping, you'll just have to be careful. Lexically scoped variables (my $variable;) have to be declared before the format (so before the use ReportFormats; in your case, while dynamically scoped (local $variable;) stuff just has to be declared in the block that calls the write() using the format.

    As usual, more information can be found in perlform.


Re: Modules of Formats
by Aristotle (Chancellor) on Oct 01, 2002 at 10:09 UTC
    I have too little experience with formats to answer your question directly, but I wonder if maybe you would like to switch to a tool like Template-Toolkit 2 instead of using formats? Managing a library of templates is pretty trivial, and in my experience you tend to modularize code better when working with a template engine - data gets munged and collected first, then passed to the template, with the two separate steps really separated. On-the-fly generation of output tends to get ugly.

    Makeshifts last the longest.

Re: Modules of Formats
by Jaap (Curate) on Oct 01, 2002 at 09:47 UTC
    and how could I go about avioding the problem of the Perl compiler not finding the variables in the formats because they're not local to the format module itself

    Take a look at other modules to see how other people solved those problems. Most variables that you use globally in your module are stored like this: $self->{'variable'} = 'value';
Re: Modules of Formats
by broquaint (Abbot) on Oct 01, 2002 at 11:00 UTC
    I think the only real problem that you might encounter is copying the actual formats about. The problem is that there's no way of directly copying them unlike the other data types e.g
    format foo = . { package mypkg; *form_foo = *main::foo; *form_ref = *form_foo{FORMAT}; } use Devel::Peek; Dump(*main::foo); Dump(*mypkg::form_foo); Dump(*mypkg::form_ref); __output__ SV = PVGV(0x80f9c78) at 0x8100de8 ... FORM = 0x80fd4e4 SV = PVGV(0x80f9dc0) at 0x8100e24 ... FORM = 0x80fd4e4 SV = PVGV(0x8101820) at 0x8100e3c ... FORM = 0x0
    So you'll have to end up exporting entire globs into the given packages, which isn't ideal.


    update: changed FORM to FORMAT

      In the year 2012-11-11 with the perl v5.12.2/v5.14.1 it does work

      $ perl 2>&1 |grep FORM FORM = 0x99aa4c FORM = 0x99aa4c FORM = 0x99aa4c

      Still doesn't quite work in v5.8.9

      FORM = 0x182fa08 FORM = 0x182fa08 FORM = 0x0

Re: Modules of Formats
by shotgunefx (Parson) on Oct 01, 2002 at 10:12 UTC
    If you create a sub that would take a hash with '$var_in_format' => \$var_to_plug_in.
    Then you could change the formats to use references to values instead of the actual value.

    You could bind the args when importing or before using them. What I'm not sure about is the binding of vars within a format. I don't know if they are bound at declaration time to the package or if when they are used. Meaning that if a FORMAT in package Formats references $system and you alias the FORMAT into package main, when used, will it use $main::system or $Formats::system, I suspect the latter but I'm not sure. Should be easy enough to test. An example of a hand rolled import can be found here


    "To be civilized is to deny one's nature."
Re: Modules of Formats
by joe++ (Friar) on Oct 01, 2002 at 11:22 UTC
    Maybe you could get away using printf/sprintf instead? This would be really trivial to put in a (OO) Module...
    $ perldoc -f sprintf $ man 3 printf

    Cheers, Joe

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://201949]
Approved by valdez
[erix]: first we take Berlin ...
[erix]: (Manhattan has fallen already)

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2017-03-28 08:45 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (328 votes). Check out past polls.