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

Does anyone know of a better way than:
print "<a href=\"test.html\">testing</a>\n",br; print FILE "<a href=\"test.html\">testing</a>\n",br;
to print text both to a browser and to a file at the same time? Ive done a little searching and was unable to find a solution.
Ted
--
"Men have become the tools of their tools."
  --Henry David Thoreau

Replies are listed 'Best First'.
Re: saving search results to file
by blazar (Canon) on May 03, 2005 at 12:03 UTC
      Thats exactly what I need! Thanks.
      Ted
      --
      "Men have become the tools of their tools."
        --Henry David Thoreau
Re: saving search results to file
by tlm (Prior) on May 03, 2005 at 12:03 UTC

    I wouldn't call this "better", but you may be able to pipe the output through "tee" to send it to both file and STDOUT with a single print statement, if that's what you want. E.g.:

    % perl -e 'open FOO, "| tee baz.txt"; print FOO "hiya\n"; close FOO' hiya % cat baz.txt hiya

    the lowliest monk

Re: saving search results to file
by TedPride (Priest) on May 03, 2005 at 13:57 UTC
    I've been using the following, but it's obviously not as elegant as tee. I need to dig into tee's guts and find out how it does what it does.
    topen('STDOUT','>file1.txt','>>file2.txt'); tprint("text\n"); tprint("text\n"); tclose(); BEGIN { my (@tp_h, $tp_s); sub topen { tclose(); for (@_) { if ($_ eq 'STDOUT') { $tp_s = 1; next; } open($_, $_) || die $!; push @tp_h, $_; } } sub tclose { close $_ for @tp_h; @tp_h = (); $tp_s = 0; } sub tprint { print @_ if $tp_s; print $_ @_ for @tp_h; } }

      IO::Tee does the same thing as what I would do:

      Tieing itself to a filehandle, and printing to all filehandles when you print to it...

Re: saving search results to file
by eXile (Priest) on May 03, 2005 at 15:24 UTC
    different (don't know if it's better, because of 'strict'-issues):
    use strict; no strict 'refs'; foreach my $fd (qw(FILE STDOUT STDERR)) { print $fd "<a href=\"test.html\">testing</a>\n"; } use strict;
Re: saving search results to file
by Anonymous Monk on May 03, 2005 at 19:31 UTC
    Why not something simple, like this?
    output(\*FILE,'<a href="test.html">testing</a><BR>\n'); sub output { my ($fh,$msg) = @_; print $msg; print $fh $msg; }
    Isn't using IO:Tee here much like smashing a fly with a hammer? Am I missing something in the original question?

    --
    AC

      Not really, this is just sample code - the actual code would be pretty ugly having to do that each time.
      print $tee "whatever";
      seems much simpler and doesnt add a lot of overhead to my code.
      Ted
      --
      "Men have become the tools of their tools."
        --Henry David Thoreau
        Not really, this is just sample code - the actual code would be pretty ugly having to do that each time.

        You're including hundreds lines of code to save typing a 3 line subroutine? That sounds like overkill to me. Put it this way: if IO::Tee breaks on you (as CPAN modules, nice as they are, occasionally do), would you rather track down the bugs in the big scary module, or the little 3 line subroutine? Me, I'll maintain the simplest thing that works, thanks! Cleverness for it's own sake is for people who like fixing bugs. ;-)

        As for "uglyness", is this:

        output $file,"whatever";
        really uglier than this? To each their own, but I don't see a substantial difference...

        print $tee,"whatever";

        It's your program, but me, I'd go for simple over "pretty", myself. Especially if I had to maintain it later.
        --
        AC

Re: saving search results to file
by TedPride (Priest) on May 03, 2005 at 16:18 UTC
    Well yes, you can do something like that, but the idea is to not have to loop through the handles manually every time you want to print.
      if you wrap this code into a subroutine, and have a global array of filehandles you wouldn't have to loop through the handles manually, this was just a showing-the-concept.

      I'd become something like:

      <open filehandles here ...> my @global_filehandles = qw(STDOUT STDERR FILE); printh("look mom, printed to 3 filehandles!\n"); sub printh { my $text = shift; foreach my $fh (@global_filehandles) { no strict 'refs'; print $fh $text; use strict; } }