#!/usr/bin/perl -w use strict; use LWP::UserAgent; #$ is a scalar variable, key-->value, LWP is a virutal browser; my $ua = LWP::UserAgent->new; my $user = 'username'; my $pass = 'password'; my $URL = 'https://www.tta.thomson.com/msi/public1_5clients.html'; #Creating a file name from the URL; my $filename = substr( $URL, rindex( $URL, "/" ) + 1 ); #Prints and /n adds new line; print "$filename\n"; #Output filename into IN; open( IN, ">$filename" ) or die $!; print "Fetching $URL\n"; my $expected_length; my $bytes_received = 0; #Fetches a file from a website; my $req = HTTP::Request->new(GET => $URL); $req->authorization_basic($user, $pass); my $res = $ua->request($req, sub { #@_ is plural of $_ 9 (default variable); my ( $chunk, $res ) = @_; # = assigns a variable, length is a length function, bytes; #_received number; $bytes_received += length($chunk); #printf is a special print function, SD Error stream, decimal; #number with percent; #symbol; unless ( defined $expected_length ) { $expected_length = $res->content_length || 0; } if ($expected_length) { printf STDERR "%d%% - ", 100 * $bytes_received / $expected_length; } print STDERR "$bytes_received bytes received\n"; # XXX Should really do something with the chunk itself print IN $chunk; } ); print $res->status_line, "\n"; #I think IN holds the file; close IN; exit;