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

Hello, I hope someone can help me. This has got to be something simple. I'm having a problem pushing output from a DOS prompt into a file. Why does this work fine:
system("dir>file");
But this doesn't work:
system("wget http://www.perlmonks.org>file");
When I try the wget, output is to the terminal rather than a file.

Replies are listed 'Best First'.
Re: capture system output
by explorer (Chaplain) on Jan 18, 2006 at 20:41 UTC
    This work:
    system("wget -o file http://www.perlmonks.org");
    You can try the tap function at the Sysadm::Install module.

      I think it's rather

      system("wget -O file http://www.perlmonks.org");
      (note the capital O).

      Update: on second thought, maybe it isn't. Depends on what you want to capture.

      I have the file output within wget working, but it is harder to manipulate the output that way. I'll try tap, thanks.
Re: capture system output
by Fletch (Bishop) on Jan 18, 2006 at 21:05 UTC

    Not that it's an answer to your difficulties with system, but rather than shelling out to wget consider LWP::Simple or LWP::UserAgent instead.

    Update: And if by "output" you mean the wget status information, that's getting written to STDERR not STDOUT so you'd need to redirect that instead.

      I'm not as interested in the actual file retrieved. What I am looking to measure is the response time, so I want to see the output from wget showing when download began and completed. This is why I would prefer to parse the output within perl rather than capturing things I don't need into a file and parsing on the backend.

        Again, just use LWP (and possibly Time::HiRes if you want finer than second granularity).

        use LWP::Simple qw( get ); use Time::HiRes qw( time ); my $url = "http://..."; my $start = time; getstore( $url, "/dev/null" ); my $end = time; print "Took ", $end - $start, " seconds\n";

        What's wrong with

        use LWP::Simple qw( get ); use Time::HiRes qw( time ); my $url = "http://www.perlmonks.org/"; my $start_time = time; get($url); my $end_time = time; print("The transfer took ", $end_time-$start_time, " seconds.\n");

        You could easily put that into a loop and take the average (but beware of caching proxies).

        Update: oops, I was a bit slow at posting this!

        My weblint program, available on CPAN, has a download timer:

        $ weblint++ -t http://www.perlmonks.com 2.315 seconds

        It's probably easier to do it yourself as some of the other posts showed you, though.

        --
        brian d foy <brian@stonehenge.com>
        Subscribe to The Perl Review