Perl Programming for Beginners
An Introduction to Learn Perl Programming with Tutorials and Hands-On Examples
Text Copyright Lightbulb Publishing
All rights reserved. No part of this guide may be reproduced in any form without permission in writing from the publisher except in the case of brief quotations embodied in critical articles or reviews.
Legal & Disclaimer
The information contained in this book and its contents is not designed to replace or take the place of any form of medical or professional advice; and is not meant to replace the need for independent medical, financial, legal or other professional advice or services, as may be required. The content and information in this book has been provided for educational and entertainment purposes only.
The content and information contained in this book has been compiled from sources deemed reliable, and it is accurate to the best of the Author's knowledge, information, and belief. However, the Author cannot guarantee its accuracy and validity and cannot be held liable for any errors and/or omissions. Further, changes are periodically made to this book as and when needed. Where appropriate and/or necessary, you must consult a professional (including but not limited to your doctor, attorney, financial advisor or such other professional advisor) before using any of the suggested remedies, techniques, or information in this book.
Upon using the contents and information contained in this book, you agree to hold harmless the Author from and against any damages, costs, and expenses, including any legal fees potentially resulting from the application of any of the information provided by this book. This disclaimer applies to any loss, damages or injury caused by the use and application, whether directly or indirectly, of any advice or information presented, whether for breach of contract, tort, negligence, personal injury, criminal intent, or under any other cause of action.
You agree to accept all risks of using the information presented in this book.
You agree that by continuing to read this book, where appropriate and/or necessary, you shall consult a professional (including but not limited to your doctor, attorney, or financial advisor or such other advisor as needed) before using any of the suggested remedies, techniques, or information in this book.
Table of Contents
1. Introduction
Perl is a general purpose high level multi-paradigm programming language. In 1987, a computer programmer named Larry Wall began working on the development of Perl language at a company called Unisys and released the first version (version 1.0) on December 18, 1987 . Perl is actually a set of two programming languages Perl 5 and Perl 6 . When we say just Perl , we refer to Perl 5 . In October 2019, Perl 6 was officially renamed to Raku . In this book, we well only focus on Perl 5 and refer to it as Perl . Although Perl does not stand for anything, there exist many unofficial acronyms including Practical Extraction and Reporting Language .
Perl is a cross-platform interpreted language that works on over 100 platforms. Being an interpreted language, there is a Perl interpreter that sits on the host system which executes Perl programs/scripts. Some of the well-known Perl supported platforms are Windows, Linux, FreeBSD, MacOS, OpenVMS, Solaris, etc . It is possible to write a Perl program on one platform (say Windows) and take it to another platform such as Linux or Mac and execute it. The execution will go ahead seamlessly as Perl is a cross platform language. One exception to this feature is that your program should not contain any platform specific code. In case of programming languages such as C/C++ and Java, there is a compiler which compiles a program and generates executable code. Perl adopts a different approach when it comes to program execution being an interpreted language, a Perl script is executed line by line by the Perl interpreter.
C programming language is used to implement Perl environment. However, no knowledge of C is needed to learn Perl . Tools and languages such as AWK, BASIC, C/C++, sed, etc . have influenced Perl and languages such as JavaScript, CoffeeScript, Python, Ruby, PHP, etc . have been influenced by Perl.
2. Scope of Perl
Perl is a general purpose scripting language and can be used for many things. You can build desktop applications, web applications, web services, GUI applications, etc. with it. There are powerful set of tools for text processing, file handling, regular expression and many more. These features led to Perls exponential growth and popularity in the 1990s and early 2000s. In 2020, Perl does not remain as popular as it used to be back in the day but is still a formidable language that can do a lot of things. In addition to the mentioned uses, Perl can be used to build network programming applications, scientific computing, system administration, etc. In fact, on Unix-like systems such as Linux, Mac and FreeBSD, system administrators use Perl to automate many tasks. Because of its power, flexibility and such diverse application space, Perl earned the nickname the Swiss Army chainsaw of scripting languages .
Another reason why Perl became so popular is because it emerged as the top choice for CGI (Common Gateway Interface) scripting . CGI is a web server interface between the end user and non-native web application. For example, a user can interact with a Perl script using a simple webpage with the help of CGI. All this cocktail of useful features, wide use cases, power and inelegance led to this language being referred to as duct tape that hold the internet together .
Database management is another powerful application of Perl. Databases such as MySQL, MSSQL, PostgreSQL, MongoDB, NoSQL, SQLite, etc. can be accessed and managed using Perl with the help of appropriate APIs.
How relevant is Perl in 2020 and beyond?
Since Perl is a general purpose, cross platform scripting language, it can be used to build applications and solve problems across many domains and platforms. Amazon uses Perl for most of its backend. There is a Perl based web framework called catalyst which powers DuckDuckGo search engines community edition and BBC iPlayers backend. Apart from catalyst, there are some more web frameworks such as Mojolicious , Perl Dancer , etc. which power thousands of websites, web applications and web services. A well-known SPAM filter called SpamAssasin is written in Perl. For developing GUI applications, there are bindings available of several cross platform GUI frameworks such as PerlQt (for Qt), PerlTk (for Tk), wxPer (for wxWidgets), etc .
What are the prerequisites of learning Perl?
If you already know programming languages such as C/C++, Python, Java, C#, etc. it will help you a great deal in learning Perl. If you do not, there is no need to worry as Perl is an easy to understand programming language. However, you should be comfortable with using your system and be well versed with using Shell/Terminal on Linux/MAC and Command Prompt/PowerShell on Windows.
What will I learn from this book?
Once you have gone through the whole book, you will be able to write simple command line based desktop applications that can do various things. This will set the base for learning advanced concepts. Perl is an incredibly powerful language for developing web applications. Learning web programming using Perl is pointless without understanding core web development technologies such as HTML5, CSS, JavaScript and hence web application development using Perl has not been covered in this book.
3. Getting Started
A PC/Laptop with Windows/Linux or a MAC system is needed to write and execute Perl scripts/programs. Perl programs can be written using any text editor including Notepad, Wordpad, vi, etc. I suggest Notepad++ (https://notepad-plus-plus.org/ ). Simple Perl scripts are plain-text files and carry the extension .pl . Other extensions of Perl file types are .pm, .xs, .t, .pod . We will only be working with the .pl file type.