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

Hello all, a slight mystery today,

I have a small project that I'm using formats with, the problem is that I want to save what the format gives back in a variable, and not printed to a Filehandle. It seems that you, well can't :) This is somewhat what I'm doing:

format TEST @<<<<<<<<<<@<<<<<<<<<< $first_name $last_name . my $first_name = 'justin'; my $last_name = 'simoni'; my $fullname = write TEST;

Just looking at the above example, you can tell it's wrong, as write... well writes to a filehandle, but the example does show what I want to do.

Is there a way to do what I want? If not... why not? I bet there's a perl module for this or something...

-justin simoni
!skazat!

Replies are listed 'Best First'.
Re: Formats and Variables
by japhy (Canon) on Jun 21, 2001 at 03:02 UTC
    You want to use formline() and $^A.
    formline('@<<<<<<<<<<@<<<<<<<<<<', $first_name, $last_name); my $fullname = $^A;
    Or, just use sprintf().

    japhy -- Perl and Regex Hacker
      Except sprintf and formline, you could also use a Safe Pipe Open, and have the child use formats, writing to STDOUT. What the child writes, the parent can read from STDIN.

      This might mean less changes in the program, depending on how things are set up. Safe Pipe Opens are discussed in the perlopentut</code> manual page.

      -- Abigail

        It's in the perlipc manpage on my system.
        --
        Snazzy tagline here
      thanks everyone, that looks like the ticket,

      -justin simoni
      !skazat!

Re: Formats and Variables
by runrig (Abbot) on Jun 21, 2001 at 03:05 UTC
    If you're writing more than one line, and want to save the accumulated output, you could use IO::Scalar. Otherwise, I'd just stick to formline or sprintf as suggested.

    Update: as pointed out below, IO::Scalar isn't going to work here...

      I am but an egg, so perhaps I am missing something very basic, but I tried to do this and it didn't want to cooperate with write (it worked just fine with print). Since the point of my exercise is to avoid having to convert a large and complicated format into, well, anything else, I really was hoping to be able to capture the output of write to a variable. (If I have to use print, I might as well convert the whole thing to lots of little $var .= "my strings go here" which would be sub-optimal for other reasons.) Here's what's been happening:
      use IO::Scalar; my $bufferVar = ""; my $SH = new IO::Scalar \$bufferVar; print $SH "Hi there, 1."; ### This works just dandy. select ($SH); print "Hi there, 2."; ### This also works. $bufferVar now contains "H +i there, 1.Hi there, 2." $~ = "myFirstFormat"; ### Allegedly this is happy. I have tested thi +s format independently, and it works fine. write ; ### This barfs.
      It says: write() on unopened filehandle IO::Scalar::FH at format_test.cgi line 29 (#1) (W unopened) An I/O operation was attempted on a filehandle that was never initialized. You need to do an open(), a sysopen(), or a socket() call, or call a constructor from the FileHandle package.

      Help?

        Sorry, I later figured out that tied handles and write don't get along (when I actually needed to write to a scalar myself and wrote this node), but I had long forgot about what I wrote above. It took this long for someone to figure out that it was wrong, so congratulations, you have graduated from egg :-)