in reply to Re: Capture wget progress bar
in thread Capture wget progress bar

Someone on Perl beginners suggested that it might be writing directly to the console and therefore not using an IO stream.
Someone else also suggested using the logging facilty of wget, and I came up with this.
sub wget { my $file = shift; $SIG{INT} = sub { unlink $log_f; exit }; if (my $pid = fork) { system "wget -o $log_f --progress=bar:force -c $file"; } else { die "cannot fork: $!" unless defined $pid; } sleep 1 until -f $log_f; open LOG, $log_f or die "Couldn't open '$log_f': $!\n"; my ($pos, $length, $status); while (1) { for ($pos = tell LOG; $_ = <LOG>; $pos = tell LOG) { s/^\s+//; if (/^Length: ([\d,]+)/) { print "Downloading: $file [$1] bytes.\n"; } elsif (/^\d+%/) { print "$_\r"; $status = 'downloading'; } elsif (defined $status eq 'downloading' and !/^\d+%/) { unlink $log_f; print "\n"; last; } } sleep 1; seek LOG, $pos, 0; } }
Of course the error handling for it like that is not too great.