Downloading Files from Web using Perl
Last Updated :
23 Jul, 2025
Perl is a multi-purpose interpreted language that is often implemented using Perl scripts that can be saved using the .pl extension and run directly using the terminal or command prompt. It is a stable, cross-platform language that was developed primarily with strong capabilities in terms of text manipulation and modifying, and extracting information from web pages. It is under active development and open source. It finds major use in web development, system administration, and even GUI development due to its capability of working with HTML, XML, and other mark-up languages. It is prominently used along with the Web as it can handle encrypted web data in addition to E-Commerce transactions.
In this article, we will be seeing different approaches to download web pages as well as images using Perl scripts.
Downloading Web Pages using Perl
Downloading a Web Page using the system command wget
In this approach, we write a sub routine where a URL is passed to a system command. The variable stores the content of the web page in the raw HTML form. We then return these contents.
Perl
#!usr/bin/perl
# using the strict pragma
use strict;
# using the warnings pragma
# to generate warnings in case of incorrect
# code
use warnings;
# specifying the Perl version
use 5.010;
# declaring the sub routine
sub getWebPage {
# variable to store the URL
my $url = 'http://www.google.com//';
# variable to store the contents of the
# web page
my $webpage = system
"wget --output-document=- $url";
# returning the contents of the web page
return $webpage;
}
# printing user friendly message
say "the contents of the downloaded web page : ";
# calling the sub routine
getWebPage();
Output:
the contents of the downloaded web page :
<raw HTML web page>
Downloading a Web Page using the system command curl
This approach is exactly the same as above, the only difference being that here the system command used is "curl" in place of "wget".
Perl
#!usr/bin/perl
# using the strict pragma
use strict;
# using the warnings pragma to
# generate warnings in case of
# erroneous code
use warnings;
# specifying the Perl version
use 5.010;
# declaring the sub routine
sub getWebPage {
# variable to store the URL
my $url = 'http://www.google.com//';
# variable to store the contents of the
# downloaded web page
my $downloadedPage = system "curl $url";
# returning the contents using the variable
return $downloadedPage;
}
# displaying a user friendly message
say "the contents of the web page : ";
# calling the sub routine
getWebPage();
Output:
the contents of the downloaded web page :
<raw HTML web page>
Downloading a Web Page using the LWP::Simple Module
LWP::Simple is a module in Perl which provides a get() that takes the URL as a parameter and returns the body of the document. It returns undef if the requested URL cannot be processed by the server.
Perl
#!usr/bin/perl
# using the strict pragma
use strict;
# using the warnings pragma to
# generate warnings in case of
# erroneous codes
use warnings;
# specifying the Perl version
use 5.010;
# calling the LWP::Simple module
use LWP::Simple;
# declaring the sub routine
sub getWebPage {
# variable to store the URL
my $url = 'http://www.google.com/';
# passing the URL to the get function
# of LWP::Simple module
my $downloadedPage = get $url;
# printing the contents of the web page
say $downloadedPage;
}
# displaying a user friendly message
say 'the contents of the web page are : ';
#calling the sub routine
getWebPage();
Output:
the contents of the downloaded web page :
<raw HTML web page>
Downloading a Web Page using HTTP::Tiny
HTTP::Tiny is a simple HTTP/1.1 client which implies it is used to get, put, delete, head (basic HTTP actions). It is used for performing simple requests without the overhead of a large framework. First, an HTTP variable is instantiated using the new operator. Next, we get the code for the request by passing the URL in the get method. On successful code, we get the length and the content of the web page at the address of the specified URL. In the case of an unsuccessful code, we display the appropriate message and mention the reasons for the failure of connection.
Perl
#!usr/bin/perl
# using the warnings pragma to
# generate warnings in case of
# erroneous code
use warnings;
# specifying the Perl version
use 5.010;
# calling the HTTP::Tiny module
use HTTP::Tiny;
# declaring the sub routine
sub getWebPage{
# variable to store the URL
my $url = 'http://www.google.com//';
# instantiating the HTTP variable
my $httpVariable = HTTP::Tiny->new;
# storing the response using the get
# method
my $response = $httpVariable->get($url);
# checking if the code returned successful
if ($response -> {success}){
# specifying the length of the
# web page content using the
# length keyword
say 'the length of the web page : ';
my $length = length $response->{content};
say $length;
# displaying the contents of the webpage
say 'the contents of the web page are : ';
my $downloadedPage = $response->{content};
say $downloadedPage;
}
# logic for when the code is
# unsuccessful
else{
# displating the reason for failed
# request
say "Failed to establish connection :
$response->{status}.$response->{reasons}";
}
}
# calling the sub routine
getWebPage();
Output:
the length of the web page :
15175
the contents of the web page are :
<html code of the web page>
Downloading multiple web pages using HTTP::Tiny
The approach for the download of multiple web pages using HTTP::Tiny is the same as mentioned above. The only modification is that here the URL of all the web pages are stored in an array and we loop through the array displaying the contents of each web page.
Perl
#!usr/bin/perl
# using the warnings pragma
# to generate warnings for
# erroneous code
use warnings;
# specifying the Perl version
use 5.010;
# calling the HTTP::Tiny module
use HTTP::Tiny;
# declaring the sub routine
sub getWebPages{
# instantiating the HTTP client
my $httpVariable = HTTP::Tiny->new;
# array of URLs
my @urls = ('http://www.google.com//',
'https://www.geeksforgeeks.org/'
);
# start of foreach loop to
# loop through the array of URLs
foreach my $singleURL (@urls){
# displaying user friendly message
say 'downloading web page...';
# variable to store the response
my $response = $httpVariable->
get($singleURL);
# logic for successful connection
if ($response->{success}){
say $singleURL.
" downloaded successfully";
# displaying the length of
# the web page
# the contents can be displayed
# similarly
say "Length : length
$response->{content}";
}
# logic for unsuccessful connection
else{
say $singleURL.
" could not be downloaded";
# displaying the reason for
# unsuccessful connection
say "$response->{status}
$response->{reasons}";
}
}
}
# calling the sub routine
getWebPages();
Output:
downloading web page...
downloaded successfully
Length : 15175
<html content of the landing page of google>
downloading web page...
downloaded successfully
Length : <Length of the landing page of GFG>
<html content of the landing page of GFG>
Downloading Images using Perl
In this section, we will see two approaches to download images using Perl scripts. In order to get the URL of these images, we first right-click on them. Next, we click on Copy Image Address from the drop-down and paste this as the URL for the image.
Downloading images using LWP::Simple
In this approach, we use LWP::Simple module and get the HTTP code using getstore function. In this function, we have to specify the URL of the image to be downloaded and the location to store the downloaded image. Next, we check if the code is successful or not and display the corresponding message to the user.
Perl
#!usr/bin/perl
# using the strict pragma
use strict;
# using the warnings pragma
# to generate warnings for
# erroneous code
use warnings;
# specifying the Perl version
use 5.010;
# calling the module
use LWP::Simple;
# declaring the sub routine
sub getImage {
# displaying a user friendly message
say "Downloading ... ";
# variable to store the status code
# first parameter is the URL of the image
# second parameter is the location
# of the downloaded image
my $statusCode = getstore
("https://www.geeksforgeeks.org/wp-content/uploads/gfg_200X200-1.png",
"downloaded_image.png");
# checking for successful
# connection
if ($statusCode == 200) {
say "Image successfully downloaded.";
}
else {
say "Image download failed.";
}
}
# calling the sub routine
getImage();
Output:
Downloading...
Image successfully downloaded.
(the downloaded image will be saved at the specified location
with the given name. If no location is specified then the image
would be saved in the current working directory.
Downloading Images using Image::Grab Module
Image::Grab is a simple module meant for downloading the images specified by their URLs. It works with images that might be hidden by some method too. In this approach, we use the Image::Grab module and after instantiating it, we pass the URL. Next, we call the grab method and save the downloaded image to disk.
Perl
#!usr/bin/perl
# using the strict pragma
use strict;
# using the warnings pragma to
# generate warnings for erroneous
# code
use warnings;
# specifying the Perl version
use 5.010;
# calling the Image::Grab module
use Image::Grab;
# instantiating the module
# and storing it in a variable
my $instantiatedImage = new Image::Grab;
# declaring the sub routine
sub getImage {
# specifying the URL
$instantiatedImage->url
('https://www.geeksforgeeks.org/wp-content/uploads/gfg_200X200-1.png');
# calling grab to grab the image
$instantiatedImage->grab;
# creating a file to store
# the downloaded image
open(DOWNLOADEDIMAGE, '>downloaded_image1.png') ||
die'downloaded_image1.png: $!';
# for MSDOS only
binmode DOWNLOADEDIMAGE;
# saving the image in the created
# file
print DOWNLOADEDIMAGE $instantiatedImage->image;
# closing the file
close instantiatedImage;
}
# calling the sub routine
getImage();
Output:
The image is stored with the specified file name.
Downloaded Image:
Similar Reads
Basics
Perl Programming LanguagePerl is a general purpose, high level interpreted and dynamic programming language. Perl supports both the procedural and Object-Oriented programming. Perl is a lot similar to C syntactically and is easy for the users who have knowledge of C, C++. Since Perl is a lot similar to other widely used lan
3 min read
Introduction to PerlPerl is a general-purpose, high level interpreted and dynamic programming language. It was developed by Larry Wall, in 1987. There is no official Full form of the Perl, but still, the most used expansion is "Practical Extraction and Reporting Language". Some of the programmers also refer Perl as the
9 min read
Perl Installation and Environment Setup in Windows, Linux, and MacOSPrerequisite: Introduction to Perl Before, we start with the process of Installing Perl on our System, whether it be Windows, Linux or Macintosh. We must have first-hand knowledge of What the Perl Language is and what it actually does?. Perl is a general purpose, high level interpreted and dynamic p
3 min read
Perl | Basic Syntax of a Perl ProgramPerl is a general purpose, high level interpreted and dynamic programming language. Perl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. Perl supports both the procedural and
10 min read
Hello World Program in PerlPerl programming language is exclusively designed for text processing purposes. Its abbreviation denotes Practical Extraction and Report Language. It is compatible on various platforms, such as Windows, Mac OS, and almost all versions of UNIX. Hello World! program in every programming language gives
3 min read
Fundamentals
Control Flow
Perl | Decision Making (if, if-else, Nestedâif, if-elsif ladder, unless, unless-else, unless-elsif)Decision Making in programming is similar to decision making in real life. In programming, a certain block of code needs to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of the program based on certain conditions. These
6 min read
Perl | Loops (for, foreach, while, do...while, until, Nested loops)Looping in programming languages is a feature which facilitates the execution of a set of instructions or functions repeatedly while some condition evaluates to true. Loops make the programmers task simpler. Perl provides the different types of loop to handle the condition based situation in the pro
7 min read
Perl | given-when Statementgiven-when statement in Perl is a substitute for long if-statements that compare a variable to several integral values. The given-when statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. given is a c
4 min read
Perl | goto statementThe goto statement in Perl is a jump statement which is sometimes also referred to as unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function. Syntax: LABEL: Statement 1; Statement 2; . . . . . Statement n; goto LABEL; In the above syntax, the
3 min read
Arrays & Lists
Perl | ArraysIn Perl, array is a special type of variable. The array is used to store the list of values and each object of the list is termed as an element. Elements can either be a number, string, or any type of scalar data including another variable. Example: @number = (50, 70, 46); @names = ("Geeks", "For",
6 min read
Perl | Array SlicesIn Perl, array is a special type of variable. The array is used to store the list of values and each object of the list is termed as an element. Elements can either be a number, string, or any type of scalar data including another variable. Arrays can store any type of data and that data can be acce
3 min read
Perl | Arrays (push, pop, shift, unshift)Perl provides various inbuilt functions to add and remove the elements in an array. .string-table { font-family: arial, sans-serif; border-collapse: collapse; border: 1px solid #5fb962; width: 100%; } .string-table td, th { background-color: #c6ebd9; border: 1px solid #5fb962; text-align: left; padd
3 min read
Perl List and its TypesIntroduction to Lists A list is a collection of scalar values. We can access the elements of a list using indexes. Index starts with 0 (0th index refers to the first element of the list). We use parenthesis and comma operators to construct a list. In Perl, scalar variables start with a $ symbol wher
4 min read
Hash
Scalars
Strings
Perl | Quoted, Interpolated and Escaped StringsA string in Perl is a scalar variable and start with a ($) sign and it can contain alphabets, numbers, special characters. The string can consist of a single word, a group of words or a multi-line paragraph. The String is defined by the user within a single quote (â) or double quote (â). Quoted Stri
4 min read
Perl | String OperatorsOperators are the foundation of any programming language. Thus, the functionality of Perl programming language is incomplete without the use of operators. A user can define operators as symbols that help to perform specific mathematical and logical computations on operands. String are scalar variabl
4 min read
Perl | String functions (length, lc, uc, index, rindex)String in Perl is a sequence of character enclosed within some kinds of quotation marks. Perl string can contain UNICODE, ASCII and escape sequence characters. Perl provides the various function to manipulate the string like any other programming language. Some string functions of Perl are as follow
4 min read
OOP Concepts
Object Oriented Programming (OOPs) in PerlObject-oriented programming: As the name suggests, Object-Oriented Programming or OOPs refers to languages that uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind to
7 min read
Perl | Classes in OOPIn this modern world, where the use of programming has moved to its maximum and has its application in each and every work of our lives, we need to adapt ourselves to such programming paradigms that are directly linked to the real-world examples. There has been a drastic change in the competitivenes
6 min read
Perl | Objects in OOPsPerl is an Objected Oriented, dynamic and interpreter based programming language. In object-oriented programming, we have three main aspects, which are, object, class, and methods. An object is a data type which can be specifically called as an instance of the class to which it belongs. It can be a
6 min read
Perl | Methods in OOPsMethods are used to access and modify the data of an object. These are the entities which are invoked with the use of objects of a class or a package itself. Methods are basically a subroutine in Perl, there is no special identity of a method. Syntax of a method is the same as that of a subroutine.
5 min read
Perl | Constructors and DestructorsConstructors Constructors in Perl subroutines returns an object which is an instance of the class. In Perl, the convention is to name the constructor "new". Unlike many other OOPs, Perl does not provide any special syntax for constructing an object. It uses Data structures(hashes, arrays, scalars) t
4 min read
Perl | Method Overriding in OOPsIn any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signat
6 min read
Perl | Inheritance in OOPsInheritance is a key concept in object-oriented programming that allows you to define a new class based on an existing class. The new class, called a subclass or derived class, inherits all of the properties and methods of the existing class, called the superclass or base class, and can also define
7 min read
Perl | Polymorphism in OOPsPolymorphism is the ability of any data to be processed in more than one form. The word itself indicates the meaning as poly means many and morphism means types. Polymorphism is one of the most important concepts of object-oriented programming languages. The most common use of polymorphism in object
4 min read
Perl | Encapsulation in OOPsEncapsulation in Perl is the process of wrapping up of data to protect it from the outside sources which need not have access to that part of the code. Encapsulation is a part of the Object-oriented programming, it is used to bind the data and the subroutines that are used to manipulate that data. I
6 min read
Regular Expressions
File Handling
Perl | File Handling IntroductionIn Perl, file handling is the process of creating, reading, writing, updating, and deleting files. Perl provides a variety of built-in functions and modules that make it easy to work with files. Here's an introduction to file handling in Perl: File modes:When opening a file in Perl, you need to spec
7 min read
Perl | Opening and Reading a FileA filehandle is an internal Perl structure that associates a physical file with a name. All filehandles have read/write access, so once filehandle is attached to a file reading/writing can be done. However, the mode in which file handle is opened is to be specified while associating a filehandle. Op
4 min read
Perl | Writing to a FileA filehandle is a variable that is used to read and write to a file. This filehandle gets associated with the file. In order to write to the file, it is opened in write mode as shown below: open (FH, â>â, âfilename.txtâ); If the file is existing then it truncates the old content of file with the
3 min read
Perl | Useful File-handling functionsPerl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. These operations can be performed by the use of various inbuilt file functions. Example: Perl #!/usr/bin/perl # Opening a
2 min read