stew has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I have been experimenting with formats and was wondering if it's possible to...
a) Define a format
b) Invoke it thru a file handle
then
c) Write the contents of the file handle to a variable rather than the file system

This is as far as I've got when I and everything seems ok

#!/usr/bin/perl format INVOICE = +--------------------------------------------------------------------- +--------+ Invoice To Invoice From + @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< The Get Rich Quick C +o. $name Address @* + $address Description: Cost: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @###.## $description £$cost +--------------------------------------------------------------------- +--------+ . open(INVOICE,">print-invoice"); $name = "Another Sucker"; $address = "Address 1\nAddress2\nXX11 9DJ"; $description = "another mug to help us swell our coffers"; $cost = 999.99; write (INVOICE);


This works fine apart from the @* denoting a multiline field y/n? gets fed into the output.(any ideas why?) But what I'd like to do is assign it to a variable to use in a template or a 'here document' for instance

I've tried adding stuff like $invoice =<INVOICE> but this doesn't seem to work

Many thanks

Stew

Replies are listed 'Best First'.
Re: FORMATS, filehandle and variables
by broquaint (Abbot) on Jan 06, 2004 at 14:43 UTC
    Perhaps you could just skip out the second step and implement something like swrite() as demonstrated in perlform e.g
    sub swrite { local $^A; formline( $_[0], @_[ 1 .. $#_ ] ); return $^A; } my $output = swrite(<<FORM, 1 .. 5 ); @<<<@|||@>>>@|||@<<< FORM print "format: $output", __output__ format: 1 2 3 4 5
    HTH

    _________
    broquaint

Re: FORMATS, filehandle and variables
by Fletch (Bishop) on Jan 06, 2004 at 15:00 UTC

    Perhaps something like IO::Scalar is what you seek?

      Or in newer perl's (5.8.0 or better I think) you could just open a handle to a scalar ref and write to it. Quick (untested) example:

      #!/usr/bin/perl format FOO = This is @||||||||||||| $foo . open(FOO,">", \$str) or die; for $foo (qw(foo bar baz)) { write FOO; } close FOO; print $str;