Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

format/write to variable

by Flame (Deacon)
on Jan 21, 2002 at 09:47 UTC ( #140316=perlquestion: print w/replies, xml ) Need Help??

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

Is there any way to channel the output from a write() command into a variable. I looked at tie() a bit, but I was hopeing there would be a more direct method.

Thanks,


-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GIT d- s:++ a--- C++++ UL P+++>++++ L+ E- W++>+++ N !o K- w+ O---- M-- V--
PS PE Y- PGP t++(+++) 5(+++)++++ X R+@ tv+ b+++ DI+ D- G e->+++ h! r-- y-
------END GEEK CODE BLOCK------
Translate

"Weird things happen, get used to it."

Flame ~ Lead Programmer: GMS

Replies are listed 'Best First'.
Re: format/write to variable
by wog (Curate) on Jan 21, 2002 at 09:54 UTC
      Not quite what I had in mind, but I suppose it would work...

      Thanks


      -----BEGIN GEEK CODE BLOCK-----
      Version: 3.12
      GIT d- s:++ a--- C++++ UL P+++>++++ L+ E- W++>+++ N !o K- w+ O---- M-- V--
      PS PE Y- PGP t++(+++) 5(+++)++++ X R+@ tv+ b+++ DI+ D- G e->+++ h! r-- y-
      ------END GEEK CODE BLOCK------
      Translate

      "Weird things happen, get used to it."

      Flame ~ Lead Programmer: GMS

Re: format/write to variable
by simon.proctor (Vicar) on Jan 21, 2002 at 14:23 UTC
    The easiest way to do this is to write your own interface to the formline function making use of the Accumulator ($^A). Look at the perlform man page for more details. As mentioned above, the man page describes a function swrite(). For reference this is it:
    sub swrite { croak "usage: swrite PICTURE ARGS" unless @_; my $format = shift; $^A = ""; formline($format,@_); return $^A; }

    To actually make use of this is a little harder because you will have to call this function repeatedly as your format changes. This will become clearer once you play with it.

    HTH
    Simon
Re: format/write to variable
by trs80 (Priest) on Jan 21, 2002 at 10:11 UTC
      Possibly IO::String will do the trick.

      IO::String or IO::Scalar always seems to be the first thing people think of, then those that try it eventually find out that format and write don't like tied filehandles. wog and simon.proctor have it right, formline and $^A are the ticket. Here is an implementation I once used when I needed to put hash data into columns.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2021-08-02 12:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (25 votes). Check out past polls.

    Notices?