Mojo :: UserAgent和进度条

时间:2013-06-29 14:13:07

标签: perl progress-bar

Mojo::UserAgent是否与LWP::UserAgent show_progress选项类似?

2 个答案:

答案 0 :(得分:2)

您可以借助progress event from Mojo::Message

制作进度条
$| = 1;

my $ua = Mojo::UserAgent->new;
$ua->on(start => sub {
    my ($ua, $tx) = @_;

    $tx->req->once(finish => sub {
        $tx->res->on(progress => sub {
            my $msg = shift;
            return unless my $len = $msg->headers->content_length;
            my $size = $msg->content->progress;
            print "\rProgress: ", $size == $len ? 100 : int($size / ($len / 100)), '%';
        });
    });
});

my $tx = $ua->get($url);
print $tx->error ? "\nDownloading failed: ".$tx->error->{message} : "\nDownloading finished!\n";

答案 1 :(得分:0)

没有

我必须提供至少30个字符,但我认为我最初的3个字符完全回答了这个问题。