Perl Tutorial

Transcription

PerlAbout the TutorialPerl is a programming language developed by Larry Wall, especially designed for textprocessing. It stands for Practical Extraction and Report Language. It runs on avariety of platforms, such as Windows, Mac OS, and the various versions of UNIX.This tutorial provides a complete understanding on Perl.AudienceThis reference has been prepared for beginners to help them understand the basic toadvanced concepts related to Perl Scripting languages.PrerequisitesBefore you start practicing with various types of examples given in this reference, we aremaking an assumption that you have prior exposure to C programming and Unix Shell.Copyright & Disclaimer Copyright 2015 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I)Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republishany contents or a part of contents of this e-book in any manner without written consentof the publisher.We strive to update the contents of our website and tutorials as timely and as precisely aspossible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of ourwebsite or its contents including this tutorial. If you discover any errors on our website orin this tutorial, please notify us at contact@tutorialspoint.comi

PerlTable of ContentsAbout the Tutorial . iAudience . iPrerequisites . iCopyright & Disclaimer . iTable of Contents . iiPART 1: PERL – BASICS . 11.Perl Introduction . 2What is Perl?. 2Perl Features. 2Perl and the Web . 3Perl is Interpreted . 32.Perl Environment . 4Unix and Linux Installation . 5Running Perl . 63.Perl Syntax Overview . 9Script Mode Programming . 94.Perl Data Types . 15Numeric Literals. 15String Literals . 165.Perl Variables . 19Creating Variables . 196.Perl Scalars . 23Scalar Operations . 257.Perl Arrays . 288.Perl Hashes . 399.Perl If Else. 45if statement . 46if.else statement . 48if.elsif.else statement . 50unless statement . 51unless.else statement . 53unless.elsif.else statement . 55switch statement . 56The ? : Operator . 5910. Perl Loops . 61while loop . 62ii

Perluntil loop . 64for loop . 66foreach loop . 68do.while loop . 70nested loops . 71Loop Control Statements . 74next statement . 74last statement . 77continue statement . 80redo statement . 82goto statement . 83The Infinite Loop . 8611. Perl Operators . 87What is an Operator? . 87Perl Arithmetic Operators . 87Perl Equality Operators . 89Perl Assignment Operators. 94Perl Bitwise Operators . 96Perl Logical Operators . 98Quote-like Operators . 100Miscellaneous Operators. 101Perl Operators Precedence. 10312. Perl Date and Time . 106GMT Time . 107Format Date & Time . 107Epoch time . 108POSIX Function strftime() . 10913. Perl Subroutines . 112Define and Call a Subroutine . 112Passing Arguments to a Subroutine . 113Passing Lists to Subroutines . 114Passing Hashes to Subroutines . 114Returning Value from a Subroutine . 115Private Variables in a Subroutine . 116Temporary Values via local() . 117State Variables via state() . 118Subroutine Call Context . 11914. Perl References . 121Create References . 121Dereferencing . 122Circular References. 123References to Functions . 124iii

Perl15. Perl Formats . 126Define a Format . 126Using the Format . 127Define a Report Header . 128Number of Lines on a Page . 131Define a Report Footer . 13116. Perl File I/O . 132Opening and Closing Files . 132Open Function . 132Sysopen Function . 134Close Function . 135The FILEHANDL Operator . 135getc Function . 136read Function . 136print Function . 136Copying Files . 137Renaming a file . 137Deleting an Existing File . 138Positioning inside a File . 138File Information . 13917. Perl Directories . 142Display all the Files . 142Create new Directory . 144Remove a directory . 144Change a Directory . 14418. Perl Error Handling . 145The if statement . 145The unless Function . 145The ternary Operator . 146The warn Function . 146The die Function . 146Errors within Modules . 146The carp Function . 147The cluck Function . 148The croak Function . 149The confess Function . 14919. Perl Special Variables . 151Special Variable Types . 152Global Scalar Special Variables . 152Global Array Special Variables . 156Global Hash Special Variables. 157Global Special Filehandles . 157Global Special Constants . 157Regular Expression Special Variables . 158Filehandle Special Variables . 158iv

Perl20. Perl Coding Standard . 16021. Perl Regular Expressions . 163The Match Operator . 163Match Operator Modifiers . 165Matching Only Once . 165Regular Expression Variables . 166The Substitution Operator . 166Substitution Operator Modifiers . 167The Translation Operator . 167Translation Operator Modifiers . 168More Complex Regular Expressions . 169Matching Boundaries . 172Selecting Alternatives . 172Grouping Matching . 173The \G Assertion . 174Regular-expression Examples . 17522. Perl Sending Email. 180Using sendmail Utility . 180Using MIME::Lite Module . 181Using SMTP Server. 184PART 2: PERL – ADVANCED TOPICS . 18523. Perl Socket Programming . 186What is a Socket? . 186Server Side Socket Calls . 187Client Side Socket Calls . 189Client - Server Example. 19024. Perl OOP in Perl . 193Object Basics. 193Defining a Class. 193Creating and Using Objects . 194Defining Methods . 195Inheritance . 197Method Overriding . 199Default Autoloading . 201Destructors and Garbage Collection. 202Object Oriented Perl Example . 20325. Perl Database Access . 206Database Connection . 207INSERT Operation . 208READ Operation . 209UPDATE Operation . 210DELETE Operation . 211v

PerlCOMMIT Operation . 212ROLLBACK Operation . 212Begin Transaction . 212AutoCommit Option . 213Automatic Error Handling . 213Disconnecting Database . 213Some Other DBI Functions . 214Methods Common to All Handles . 21626. Perl CGI Programming . 218What is CGI ? . 218Web Browsing . 218CGI Architecture Diagram . 219Web Server Support and Configuration . 219First CGI Program. 219Understanding HTTP Header . 220CGI Environment Variables . 221GET and POST Methods . 223Passing Information using GET Method .

Perl is an Open Source software, licensed under its Artistic License, or the GNU General Public License (GPL). Perl was created by Larry Wall. Perl 1.0 was released to usenet's alt.comp.sources in 1987. At the time of writing this tutorial, the latest version of perl was