how to load 'formats' from a file

Discussion in 'Perl Misc' started by simulant, Nov 8, 2006.

  1. simulant

    simulant Guest

    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
    ..
     
    simulant, Nov 8, 2006
    #1
    1. Advertising

  2. simulant

    Ben Morrow Guest

    Quoth "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:

    [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

    --
    If I were a butterfly I'd live for a day, / I would be free, just blowing away.
    This cruel country has driven me down / Teased me and lied, teased me and lied.
    I've only sad stories to tell to this town: / My dreams have withered and died.
    (Kate Rusby)
     
    Ben Morrow, Nov 8, 2006
    #2
    1. Advertising

  3. simulant

    -berlin.de Guest

    simulant <> wrote in comp.lang.perl.misc:
    > 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
     
    -berlin.de, Nov 8, 2006
    #3
  4. simulant

    Ben Morrow Guest

    Quoth -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
     
    Ben Morrow, Nov 9, 2006
    #4
  5. simulant

    -berlin.de Guest

    Ben Morrow <> wrote in comp.lang.perl.misc:
    > Quoth -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
     
    -berlin.de, Nov 9, 2006
    #5
  6. simulant

    simulant Guest

    Thanks to all who answered. Storing the formats in their own modules
    is working now as intended!
     
    simulant, Dec 8, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page