in reply to print to data

Besides them two, you could define your custom print routine, e.g. cprint for conditional print.
# set up an array at the top of your program my @output; # set up a flag my $capture = 0; sub cprint { $capture ? (print @_) : push(@output,@_); }
and change all your print statements to cprint. Setting $capture to 1 results in filling the array @output.

Another hint: Don't use - even for quick hacks - the DATA filehandle for regular files, the DATA filehandle is special. See e.g. perldata.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: print to data
by Yoda_Oz (Sexton) on Sep 06, 2006 at 12:15 UTC
    i dont quite understand the custom print routine you just described above. can you explain in a little bit more detail please. thanks.
      sub cprint { $capture ? (print @_) : push(@output,@_); }

      is short for

      sub cprint { if($capture) { # if $capture is != 0, '' or undef print @_; # print what's passed in } else { # otherwise push(@output,@_); # stuff goes into @output } }

      See the section Conditional Operator in perlop for $foo ? $bar : $quux.

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}