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

I use below code to print out my result, it works fine. I'd like to print it to a file, Can I? How?
format TOP_STDOUT= NAME ID SALARY . format STDOUT= @<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<<<< $name, $id, $salary . write.
Thanks in advance!!

Replies are listed 'Best First'.
Re: Can I print the result to a file
by chromatic (Archbishop) on Apr 30, 2003 at 23:23 UTC

    If you have control over the shell, simply redirect output to a file or another process:

    $ perl myprog.pl > output $ perl myprog.pl | less

    If you need to print to a file from within the program, you'll have to close STDOUT and reopen it to a file. The shell's the way to do this.

      Or instead of reopening STDOUT you can change the format name to the name of the filehandle you want to print out to.

      open (FOO, "foo"); format FOO= blah blah write FOO;

      This will assume that you want to print format FOO to the filehandle FOO.

      Cheers

Re: Can I print the result to a file
by atnonis (Monk) on Apr 30, 2003 at 23:39 UTC
    Hi there!
    by adding the following code after write;:
    select TOP_STDOUT; print;
    with select(); you are selecting the default output of print statements
    or you can can pass format's into an variable like:
    my $format = "format TOP_STDOUT= NAME ID SALARY . format STDOUT= @<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<<<< $name, $id, $salary . write;" print TOP_STDOUT $format;

    Hope that helps!

    Antonis!