how to load 'formats' from a file

S

simulant

I am using the `format` command in a reporting module I am writing, and
I want to be able to store multiple format pictures in files and then
load the needed format from the file. This would be an example session
using my modules:

$rpt = new Report();
$rpt->title("My Title");
$rpt->formattop("header1.fmt"); # there would be multiple headers to
choose from
$rpt->format("format1.fmt"); # format1.fmt would be stored in
/usr/lib/perl/formats, for instance
$rpt->run(); # generate the content of the report
$rpt->write("report.out"); # this writes the output using the format
specified in 'format1.fmt'

header1.fmt:
Title: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$title
Date: @>>>>>>>>>>>>>>>>>>>
$now

..

format1.fmt:
@*
$content
..

The Report module would look like this:

package Report;

sub new {
# code for constructor ...
}

sub title {
# code to set title
}

sub format {
# code to set format variable
}

sub run {
# this sets the content of the report
}

# arg to write is filename (report.out)
sub write {
<b># this is where I need help!!</b>
my $self = shift;
open ($self->format, ">", shift);
$title = $self->title;
$now = $self->getDateTime;
$content = $self->run;

write $self->format;
close $self->format;
}


Nowhere in perlform or anywhere else I searched was there a mention of
how to load formats from files. The code above in the write sub does
NOT work, but I would like to do something similar.

I have the formats defined at the top of the module for now, and this
works, but this does not support dynamically loading different formats:

format FORMAT1_TOP =
Title: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$title
Date: @>>>>>>>>>>>>>>>>>>>
$now

..

format FORMAT1 =
@*
$content
..
 
B

Ben Morrow

Quoth "simulant said:
I am using the `format` command in a reporting module I am writing, and
I want to be able to store multiple format pictures in files and then
load the needed format from the file. This would be an example session
using my modules: [snip]
Nowhere in perlform or anywhere else I searched was there a mention of
how to load formats from files. The code above in the write sub does
NOT work, but I would like to do something similar.

I have the formats defined at the top of the module for now, and this
works, but this does not support dynamically loading different formats:
[snip]

See the swrite function defined at the end of perlform. Basically, you
can't load formats from a file; you have to use formline and $^A to
format the data 'by hand'.

Alternatively, I would recommend the Perl6::Form module from CPAN. It
implements the Perl6 'form' command for Perl5, which is both much more
flexible and has considerably nicer formatting options.

Ben
 
A

anno4000

simulant said:
I am using the `format` command in a reporting module I am writing, and
I want to be able to store multiple format pictures in files and then
load the needed format from the file. This would be an example session
using my modules:
[snip]

Nowhere in perlform or anywhere else I searched was there a mention of
how to load formats from files. The code above in the write sub does
NOT work, but I would like to do something similar.

That's because formats weren't designed to be loaded dynamically.
A format must be defined at compile-time, there's no way around
that.
I have the formats defined at the top of the module for now, and this
works, but this does not support dynamically loading different formats:

format FORMAT1_TOP =
Title: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$title
Date: @>>>>>>>>>>>>>>>>>>>
$now

.

format FORMAT1 =
@*
$content
.

The simplest way to load formats dynamically would be to put the
various format statements in modules and load the one you want. They
should all refer to the same file handle, say DYN_FORMAT so the main
program knows what to open. Otherwise, put your code above literally
into a file named Format1.pl. Don't forget to add a final statement
"1;".

The main program could do (untested)

our ( $title, $now, $content) =
( 'heading', scalar localtime, 'stuff');

use Format1;

open DYN_FORMAT, '>&', 'STDOUT' or die $!;
write DYN_FORMAT;

Make a copy Format2.pl of Format1.pl and edit it to contain a different
format. Change "use Format1" to "use Format2" in the main program,
which will now use the other format.

Anno
 
B

Ben Morrow

Quoth (e-mail address removed)-berlin.de:
The simplest way to load formats dynamically would be to put the
various format statements in modules and load the one you want. They
should all refer to the same file handle, say DYN_FORMAT so the main
program knows what to open.

....and, of course, you have to bear in mind that formats, as
filehandles, are package-scoped.
Otherwise, put your code above literally
into a file named Format1.pl. Don't forget to add a final statement
"1;".

The main program could do (untested)

our ( $title, $now, $content) =
( 'heading', scalar localtime, 'stuff');

use Format1;

Umm, either you mean

require 'Format1.pl';

or you meant Format1.pm above.
open DYN_FORMAT, '>&', 'STDOUT' or die $!;
write DYN_FORMAT;

In fact, there's no need for separate files. This

#!/usr/bin/perl

(my $f = shift) =~ s/!/\n/g;

our ($x, $y) = qw/aaa bbb/;

eval <<EOFORMAT;
format FOO =
$f
 
A

anno4000

Ben Morrow said:
Quoth (e-mail address removed)-berlin.de:
[...]
Otherwise, put your code above literally
into a file named Format1.pl. Don't forget to add a final statement
"1;".

The main program could do (untested)

our ( $title, $now, $content) =
( 'heading', scalar localtime, 'stuff');

use Format1;

Umm, either you mean

require 'Format1.pl';

or you meant Format1.pm above.

Thanks, the latter.

Anno
 
S

simulant

Thanks to all who answered. Storing the formats in their own modules
is working now as intended!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top