Title: The Perl Scripting Language Author: David Stotts Affiliation: Department of Computer Science University of North Carolina at Chapel Hill
[email protected] Final Draft: December 20, 2002
Outline 1 Introduction 2 A Brief History of Perl 3 Perl Language Overview 3.1 Basic Data Types and Values 3.2 Basic operations 3.3 Control Flow 3.4 Subroutines 3.5 Regular Expressions and Pattern Matching 3.6 Input/output and File Handling 3.7 Other Perl features 4 Putting it All Together: Sample Programs 4.1 First example: text processing 4.2 A Simpler, More Sophisticated Example 4.3 Directory information processing 5 Network Programming in Perl 5.1 Perl Modules and CPAN 5.2 Web Server Scripts with CGI 5.3 Web Clients with LWP 5.4 Database Use 5.5 Processes and IPC 6 On Beyond Perl 6.1 Python 6.2 Ruby 7 For More Information
Glossary CGI Common Gateway Interface, standard for Web server scripting CPAN Comprehensive Perl Archive Network, repository for useful Perl code and documentation Hash Associate array, a collection of data items indexed by string (scalar) values HTTP HyperText Transfer Protocol, encoding standard for Web transactions HTML HyperText Markup Language, language for specifying Web page content IPC Interprocess Communication Linux Popular open source version of Unix for PCs Pipe Unix name for a communication channel between processes Regular expression formal language for specifiying complex patterns and strings of characters Scalar Singular data value such as integer, string, boolean, real Unix Popular operating system developed at Bell Labs, UC Berkeley in the late 70’s
Abstract This article gives an overview of the Perl scripting language and shows how it is used for programming Internet and Web applications. The history of the language is presented along with its Unix heritage. Basic language features are explained with examples of Perl scripts for string manipulation, pattern matching, text processing, and system interactions. The Perl community module repository CPAN is discussed, and several of the popular Perl modules are used to illustrate Web client, Web server, database, and network programming. Two newer scripting languages – Python and Ruby – are briefly discussed and compared to Perl. Finally, many of Perl’s most advanced features are mentioned but not discussed, so references are provided for readers who wish further study.
1 Introduction From its introduction to the programming community in 1987, Perl has become today one of the most widely known and used programming languages. Designed by Larry Wall, and originally thought of as a natural enhancement for the popular csh shell script notation of Unix, Perl was at first primarily used for text manipulation. Its maturity in the early 90’s coincided with the rise of the Web, and it rapidly became the most popular programming language for HTML form processing and other Web development as well. Perl has been called a “Swiss Army chainsaw” for its plethora of features coupled with its considerable programming power and flexibility. The common phrase among hardened Perl programmers is “there’s more than one way to do it.” Most programming goals can be achieved in Perl in at least three ways, depending on which language features and techniques the programmer prefers to use. It is not uncommon for an experienced Perl programmer to reach for the manual when reading code written by another programmer. Perl has also been called “duct tape for the Web” emphasizing its utility for producing applications, web sites, and general program fixes for a wide variety of problems and domains. In this article we give a brief history of Perl, including major events preceding Perl that set the historical stage for it. We provide an overview of the language