http://www.perlmonks.org?node_id=885186

michal.d has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have a question, I am new in perl, and right now I am trying format my output, I am using own defined formats. I can't find anywhere how can I put output from write; function to string variable. Or maybe there are better way to format strings.

Replies are listed 'Best First'.
Re: redirect stream from wirte; to $string
by ELISHEVA (Prior) on Jan 31, 2011 at 05:54 UTC

    My eyes always miss this in the documentation, but starting in Perl 8.8 or thereabouts you can make a string into a file handle and then select it as the default file handle for write:

    #make string into file handle my $sBuf=''; open(my $fhOut, '>', \$sBuf); #\$sBuf is a reference to $sBuf # make it your output stream for format/write select $fhOut; my ($sName, $iAge); format SomeFormat = Test: @<<<<<<<<<<<< @>>>>> $sName, int($iAge) . $sName = "MickeyMouse"; $iAge = 82; #born Nov, 1928 $~ = 'SomeFormat'; write; print "buffer is: <$sBuf>\n";

    If you have perl 5.8.8 installed the documentation only shows up in open and not in perlopentut. In the Perl 5.12.2 documentation, there is a FAQ question devoted to this.

    Alternatively, you can also try to follow the advice here to roll your own swrite() function.

    Update: added sample code using string buffer file handle with format and write

    Update: added the 5's, as per DrHyde below.

      You do of course mean perl *5*.8.8 and *5*.12.2.

      There's also IO::Scalar and IO::String on the CPAN.

Re: redirect stream from wirte; to $string
by thezip (Vicar) on Jan 31, 2011 at 04:48 UTC

    sprintf and printf are your huckleberries...

    The first argument is the format specification, which is then followed by a list of variables that directly correspond to the "placeholders" specified in the format.

    For example,

    printf "%05d", 32; # prints an integer 5 digits wide, with leading ze +roes, ie. 00032 printf "%32s", 'foo'; # prints the word 'foo' into an area 32 characte +rs wide

    This doesn't even begin to scratch the surface of what sprint and printf do, but it is a place to start.


    What can be asserted without proof can be dismissed without proof. - Christopher Hitchens
Re: redirect stream from wirte; to $string
by GrandFather (Saint) on Jan 31, 2011 at 02:33 UTC

    Show us the code you have tried.

    True laziness is hard work
Re: redirect stream from wirte; to $string
by michal.d (Initiate) on Jan 31, 2011 at 11:27 UTC
    Thank you all My code :
    format SIMPLE_TOP= ______________________________________________________________________ +_ @||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| "TITLE" ______________________________________________________________________ +_ @|||||||||||||||||... |@||||||||... @||||||||... |@||||||||||||||||||. +.. "Variable1","Variable2","Variable3","Variable3" ______________________________________________________________________ +_ . format SIMPLE= @<<<<<<<<<<<<<<<<<... |@||||||||... @||||||||... |@||||||||||||||||||. +.. $varible, $varible, $varible, $varible ______________________________________________________________________ +_ .
    and I am using write; to print it in the output. I found in documentation :
    use Carp; sub swrite { croak "usage: swrite PICTURE ARGS" unless @_; my $format = shift; $^A = ""; formline($format,@_); return $^A; } $string = swrite(<<'END', 1, 2, 3); Check me out @<<< @||| @>>> END print $string;
    but I don't have idea how it schould work, could you explain me, what mean << operator in that function, and how can I put more complicated formats, with TOP, and , FOOTER etc. ELISHEVA and DrHyde , I will try your tips, and check which will work for me better. thanks a lot for respond.
      Eep! Formats! I haven't seen those in YEARS! In fact I've not seen them used for nearly two *decades*! You should consider them to be obsolete, and find another way of formatting your output. sprintf() is probably the weapon of choice.
        I just looking for way, to format somehow my output for easily reading. And I find those formats, I will try sprintf(),