Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

FORMAT Trouble

by SolidState (Scribe)
on Jul 05, 2006 at 05:43 UTC ( #559248=perlquestion: print w/replies, xml ) Need Help??

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

Hi, Can anyone tell me if it is possible to define 2 different formats for the same filehandle?

The reason I am asking is that I want to print 2 different tables to the same text file and I don't want to use printf statements. For me at least, code that uses printf to print something as complex as a text table is hard to both write and read, hard to understand and hard to debug. It's also ugly, to boot :)

My current workaround is to close the file, reopen in append mode with a different filehandle name and define the second format with this name. Ugly, but works. Any other/better solutions?

Thanks in advance,
SolidState

Replies are listed 'Best First'.
Re: FORMAT Trouble
by bobf (Monsignor) on Jul 05, 2006 at 06:04 UTC

    It's a big ugly, but you can use select and $~ to change the format for a given filehandle. See perlform for more info. In the meantime, here is a brief example:

    use strict; use warnings; open( my $outfh, '>', 'temp.txt' ) or die $!; my ( $var1, $var2 ) = ( 'one', 'two' ); my $oldfilehandle = select $outfh; $~ = "FORMAT1"; select $oldfilehandle; write $outfh; $oldfilehandle = select $outfh; $~ = "FORMAT2"; select $oldfilehandle; write $outfh; $oldfilehandle = select $outfh; $~ = "FORMAT1"; select $oldfilehandle; write $outfh; format FORMAT1 = @<<<<< @<<<<< $var1, $var2 . format FORMAT2 = @<<<<<XXX@<<<<< $var2, $var1 . close $outfh;
    And the output file:
    one two two XXXone one two

      This is exactly what I wanted! I read perlform, but didn't understand it. Your example it so much clearer!

      Thanks!
Re: FORMAT Trouble
by shmem (Chancellor) on Jul 05, 2006 at 06:12 UTC
    First thing to do, read perlform and perlvar :-)

    Just select the format you like assigning to $~. Example:

    #!/usr/bin/perl our(@l,$c); while(@l = getpwent) { $~ = $c++ % 2 ? 'foo' : 'bar'; write; } format foo = left @<<<<<<<<<<<< @<<<<<<<<<<<< $l[0],$l[7] . format bar = right @>>>>>>>>>>> @>>>>>>>>>>>> $l[0],$l[7] .
    _($_=" "x(1<<5)."?\n".q/)Oo.  G\        /
                                  /\_/(q    /
    ----------------------------  \__(m.====.(_("always off the crowd"))."
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: FORMAT Trouble
by diotalevi (Canon) on Jul 05, 2006 at 05:48 UTC

    I kind of doubt you can do what you asked for. The replacement for that in perl 6 is also available in perl 5 with Perl6::Form.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://559248]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2023-06-07 21:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you go to conferences?






    Results (29 votes). Check out past polls.

    Notices?