in reply to format and IO::String


As runrig and simon.proctor pointed out in the last node the best way to get formatted text into a variable is to use formline and the accumulator $^A.

Also, as runrig pointed out write won't work directly with IO::Scalar.

The following is a example of putting formatted text into a variable. If you really need it in a IO::Scalar you can transfer it on from there.

#!/usr/bin/perl -w use strict; my $temp; my $format = '@||| @||| @|||'; my $t1 = '1'; my $t2 = '2'; my $t3 = '3'; # Create the format formline($format, $t1, $t2, $t3); $temp = $^A; # Store the accumulator $^A = ''; # Clear the accumulator print $temp;

--
John.

Replies are listed 'Best First'.
Re: Re: format and IO::String
by Flame (Deacon) on Jan 31, 2002 at 00:05 UTC
    I'd really prefer to be able to use a format declaration, because the formats I'll need to use are usually multiline and I don't want to have to go into all that complicated stuff when I decided to use format to simplify things to begin with.

    I've been experementing with pipe, but it keeps locking up... Any suggestions?

    This code seems to work:
    pipe(TESTOUT,TEST) or die($!); my $t1 = '1'; my $t2 = '2'; my $t3 = '3'; select(TEST); $|=1; print "Testing..."; write(); close(TEST); select(STDOUT); my @temp = <TESTOUT>; print join('',@temp); format TEST= @||| @||| @||| $t1,$t2,$t3 @||| @||| @||| $t3,$t2,$t1 .


    However, when I try a longer format, it tends to freeze:

    my $GMS = new GMS; my $sitename = $GMS->getsetting('GUILD','SITENAMET'); my($name,$email,$icq) = ('Iron','flame@berkshire.rr.com','16307443'); my @stats = (100,100,25); my $skills = [ {'SKILL' => 'Anatomy','VALUE' => 100}, {'SKILL' => 'Swordsmanship','VALUE' => 100}, {'SKILL' => 'Tactics','VALUE' => 100} ]; my $notes = "This is a long test string.\n\n" x 10; pipe(GMMAILO,GMMAIL) or die($!); select(GMMAIL); $|=1; print "Testing..."; write();# or die($!); close(GMMAIL); select(STDOUT); my @temp = <GMMAILO>; print join('',@temp);




    "Weird things happen, get used to it."

    Flame ~ Lead Programmer: GMS