What does this book cover? Installing Perl on Windows and UNIX; Making use of online Perl resources like CPAN; First principles in programming and the Perl. Perl i. About the Tutorial. Perl is a programming language developed by Larry Wall, especially designed for text processing. It stands for Practical Extraction and . Perl Tutorial in PDF - Learn Perl in simple and easy steps starting from basic to advanced concepts with examples including Introduction, Environment, Syntax.
|Language:||English, Spanish, Portuguese|
|PDF File Size:||16.48 MB|
|Distribution:||Free* [*Regsitration Required]|
Teach Yourself Perl 5 in 21 days. David Till. Table of Contents: Introduction q Who Should Read This Book? q Special Features of This Book q Programming. Programming Perl (Wall, Christiansen, & Schwartz). Perl in a Nutshell (Siever, Spainhour, & Patwardian). Perl Mongers meteolille.info Comprehensive Perl. Of course, if your job is programming, you can get your job done with any " complete" Perl has been fueled by the hankerings of former UNIX programmers who.
Perl is a high-level, interpreted, dynamic programming language. Did it all sound Greek to you? Unless you actually know Greek. Perl is a programming language specially designed for text editing. It is now widely used for a variety of purposes including Linux system administration, network programming, web development, etc. Let's put it in a simple manner. Perl is a programming language which uses natural language elements, words that are used in common English language and is, therefore, easier to understand by humans [ high-level language ].
Now there's a problem; computers cannot understand high-level languages, which we humans can easily understand. For that, we need something which can translate the high-level language to low-level language.
Here interpreter comes to our help. The interpreter is a piece of software which converts the program written in the high-level language to low-level language for the computer to understand and execute the instructions written in the program.
Hence, Perl is an interpreted programming language. What You Will Learn: The power of Perl can be implemented in many fields. The most popular use of Perl is in Web development.
Perl is still used for its original purpose i. It can produce reports on resource use and check for security issues in a network. Due to this reason, Perl has become a popular language used in web development, networking and bioinformatics too. Apart from all this perl can also be used for CGI programming. Apart from that networking via telnet, FTP, etc. Perl is flexible enough to support Procedural as well as OOP practices simultaneously.
This means that you can combine Perl with these extra programming languages rather rewriting existing code. Why use Perl? It is true that there are other programming languages that can be used to do all the stuff that has been stated above, then why should you specifically use Perl?
Perl is very easy to learn , especially if you have a background in computer programming. Perl was designed to be easy for humans to write and understand rather than making it easy for processing by computers. It uses regular expressions. It's natural style of language is different from other programming languages that use specific grammar and syntaxes; therefore, Perl is very flexible and doesn't impose on you any particular way of thinking out a solution or a problem.
Perl is extremely portable. It can run on any operating system that has Perl interpreter installed, so it is platform independent. This is unlike Shell scripts, where the code changes with the flavor of Linux distribution being used, making it less and less portable Small specific tasks in Perl become very easy and quick.
Throughout this tutorial, you will learn how you can code small, quick programs for specific tasks. Let's take a simple example of the classic Hello World program which is used to begin learning any programming language which has UNIX as its roots: Perl hello world! Hello, world! The above two lines of code will print Hello, world!
Now wasn't it too simple and quick? You might be wondering why Perl is so famous on the Web. It is simple as most of the things that happen on the web are of TEXT and Perl is very good at text processing.
If we compare Perl with any of the languages, then Perl will be the best language which is good in File handling, text processing, and output reporting One of the best advantages of Perl is that it is free to use The Perl community strongly believes that software should be freely available, freely modifiable and freely distributable.
Several volunteers from Perl community strive to make the programming language as good as possible. Compared to other Programming languages Perl is most powerful for text handling and Parsing This is an interpreted language with fast execution time as there is no need to compile a Perl script Simple and easy to program and understand.
It is object oriented. CPAN library eases Perl development we will learn more about this later in the subject. Used in Web development for mostly Payment Gateways. Used in Automation and to test most of the Network and Storage related stuff. There is minimal GUI support as compared to other Programming languages.
You need to refer to complex library modules which are not so easy to understand Don't be scared once you know how to do the things everything will be easy. Understanding complex Patterns requires experience. Let's get started With enough knowledge about the history of Perl and basic concepts of computer programming required for coding in Perl, it is time we take the dive and get started with Perl.
The next chapter will teach you how you can setup Perl on your system and get ready for the coding journey in Perl. The tutorial will take Linux as the OS which students will be using for coding in Perl. Good news you probably have it!
But if you can't find it already on your system, you can still get it for free. To find out if you already have Perl installed, go into the command line and type: In this case, the version is v5.
But if not… don't panic… Unix Already Installed Comes pre-installed with Perl though you may need to update to the latest version. If you need to update the Perl version then just enter one single line of command sudo apt-get install perl and relax. The rest will be taken care of. Just make sure you have an active internet connection. Install perl for Windows: First, download the Active Perl from this link. Follow these steps to install ActivePerl on Windows system.
See the below screenshots for the same. Step 1: Once you download the installer and start the installation you will see the below window, click on next to proceed. Step 2: Accept Licensing agreement to proceed the installation.
Step 3: Below are different packages that will be installed. By default, all will be selected. This is the utility provided by Active Perl to install external Perl modules or libraries in your system. Click on Next to proceed. Step 4: These are different types of Perl extensions that can be used for Perl. Mostly we will be using. Plx and. Pm for Perl. Perl modules basically use. Pm as their file extension to refer to a library file.
Select all the options and click on the Next button.
Step 5: Click on Install button to proceed with the installation. Step 6: Once installed, execute the command 'Perl —v' to check whether Perl is successfully installed in your system. There are lots of things which needs to be discussed for setting Perl environment in both Linux and Windows, as there won't be many library files included in this installation. You need to manually install those. But these files are not mandatory to start coding in Perl. Perl Example: Hello World! Everything will be clear soon.
Come on and take a look at it by line: If it's essential, you must write here a path to your interpreter. This special line must be at the beginning of your program and must starts from! The semicolon at the end of the line tells the perl interpreter that the instruction is finished. You must put a semicolon at the end of every instruction in Perl code. Note the quotation mark ". It's necessary for print instruction. Save the above script as firstprog. You can also run command-line interface and write on the console: Type in the Terminal: To execute, write: If you use perl modules, you do not have to rewrite the codes to perform that same operation.
Perl can make use of such external libraries of codes. One of the best libraries is the CPAN. It stands for Comprehensive Perl Archive Network and contains a vast amount of Perl modules for you to use. It is a community or network of a large number of developers who contribute such modules. Most Perl modules are written in Perl, some use XS they are written in C so require a C compiler it's easy to get this setup - don't panic.
Modules may have dependencies on other modules almost always on CPAN and cannot be installed without them or without a specific version of them. It is worth thoroughly reading the documentation for the options below. Install cpanminus a script to get, unpack, build and install modules from CPAN to make installing other modules easier you'll thank us later. To Install App-cpanminus perl modules, in the command line type: Now install any module: Let's see an example of installing a module File:: Data It's an Interface to access File Data.
Perl Variable Now, we'll talk about variables. You can imagine variable like kind of container which holds one or more values. Once defined, the name of variable remains the same, but the value or values change over and over again. There are 3 Types of variables: The easiest ones are scalars, and this is ours today subject Scalar Variable This type of variable holds a single value. Its name begins with a dollar sign and a Perl identifier it's the name of our variable. Naming Convention If you are familiar with other programming languages, then you would know that there are certain rules about naming variables.
Similarly, Perl has three rules for naming scalars. Like PHP. Underscore character is also allowed. Use underscore to split the variable names into two words. Examples Perl Example: Scalars can be read from devices, and we can use it to our programs. In this type of scalar data we could specify: In general, Perl interpreter sees integers like floating points numbers. For example, if you write 2 in your programs, Perl will see it like 2.
It consists of one or more digits, optionally preceded by a plus or minus and containing underscores. Perl Examples: But believe me, this is the most common type of scalars.
They're everywhere. Floating-point literals: It consists of digits, optionally minus, decimal point and exponent. It's alternative to the decimal system. Allow me to show you Octal, Hexadecimal and Binary representation. A short table presents all important information about this weird styles: Perl doesn't store the values in the same format.
It will internally convert these hexadecimal, binary, octal to decimal values. The assignment is the most common operation on a scalar, and it's very simple. Perl uses for that equal sign. It takes a value of the expression from the right side and puts this value to our variable. Let's take a look to examples: It's also very simple type of scalar.
The maximum length of a string in Perl depends upon the amount of memory the computer has. There is no limit to the size of the string, any amount of characters, symbols, or words can make up your strings.
The shortest string has no characters. The longest can fill all of the system memory.
Perl also permits you to add any 8-bit or bit character set aka. Perl has also added support for Unicode UTF Like numbers there are two different types of strings: Single quotes string literals Double quotes string literals Single-quoted string literals Single quotation marks are used to enclose data you want to be taken literally. A short example and everything should be clear: That means that escaped characters and variables aren't simply literally inserted into later operations, but are evaluated on the spot.
Escape characters can be used to insert newlines, tabs, etc. On the other hand, you can use double-quotes to interpolate slices of both arrays and hashes. Yes, they are! But don't worry- just a few. Perl gets our string from the right side of the equal sign and puts this string to a variable. There isn't class of variables. String Concatenation period: The concatenation operator ". If the string contains quotes, carriage returns, backslashes, all these special characters need to be escaped with a backslash.
Perl ' ' variable Examples: In the end… Conversion Between Numbers and Strings: As you know, Perl automatically converts between numbers to string as needed. How does Perl know, what we need right now? This is simple- everything depends on the operator we'll talk about operators later, now, just accept there are a lot of operators, different for numbers and strings If an operator expects a number, Perl will use the value as a number.
If an operator expects a string, Perl will use the value as a string. In other words- you don't need to worry about this kind of conversion. Short example and everything should be clear: Scope of a variable — Access Modifiers We can declare a scalar in anywhere in the program. But you need to specify an access modifier There are 3 types of modifiers my local our My: Using this you can declare any variable which is specific within the block.
The variable, which is declared outside the block will be accessible to if block but the variable which was declared inside if block won't be accessible to the outer program. Let's see how this works with an example.
Once a variable is declared with access modifier "our" it can be used across the entire package. Suppose, you have Perl module or a package test. This variable can be accessed in any scripts which will use that package. If you are serious about programming in Perl, you should begin your program with! This is a good programming practice!
An Array is a special type of variable which stores data in the form of a list; each element can be accessed using the index number which will be unique for each and every element. You can store numbers, strings, floating values, etc.
This looks great, So how do we create an array in Perl? In Perl, you can define an array using ' ' character followed by the name that you want to give. Let's consider defining an array in Perl.
There are different ways of storing data in an array. This depends on how you are going to use it. The array index starts from 0 and ends to its maximum declared size, in this case, the max index size is 3. You can also declare an array in the above way; the only difference is, it stores data into an array considering a white space to be the delimiter.
Here, qw means quote word. The significance of this function is to generate a list of words. You can use the qw in multiple ways to declare an array. Suppose, you want to store numbers or alphabets a-z in an array. Yes, we can. What is Dynamic Array? Dynamic arrays are those that you declare without specifying any value on them. So when exactly do we store values in that array? Simple, we store them during run time. Here is a simple program for that.
We will be using some inbuilt Perl functions for doing this task. This is kind of dyn mic rr y The split function splits the content of string into an array based on the delimiter provided to it. This function will also eliminate the delimiter from the string, in this case, it is 'a'; Push, Pop, shift, unshift for Perl arrays: Perl Push: Perl Pop: Perl Shift: Perl Unshift: Let's see an example where we can use the below functions.
Mon Tue Wed 2nd when push: Mon Tue Wed Thu 3rd when unshift: Fri Mon Tue Wed Thu 4th when pop: Fri Mon Tue Wed 5th when shift: We already learned about scalars and arrays in previous sections.
What exactly scalars do? It only stores integers and strings. What exactly arrays do? It is a collection of scalars, where you access each element of an array using indices.
But, is this good idea to use an array when you have hundreds and thousands of records? We will forget which index has what value. To overcome this situation we have something like perl hash. What are Hashes? A hash can also hold as many scalars as the array can hold.
The only difference is we don't have any index rather we have keys and values. Let's see an example how we can define a Perl hash and how we can differentiate this from array Consider an example of three people and their ages are represented in an array. Sainath33Krishna24Shruthi25 This way it is difficult to know individual ages as we need to remember both index locations of name and age of all the people. It may be simple when you have 3 names, but when you have or more?
You know the answer. This we can overcome using a hash. Hash Example: But, how do we access or print it? Each element in hash should be accessed with its associated key for which there will be a value assigned. So, there is a one-one mapping between each of the key and value in the hash.
In order to print the age of any person, you just need to remember the name of that person.
Each Key in hash should be unique or else it will override your value, which was assigned previously. How can we assign a hash to another hash? Simple, same way as we do for We can also print entire hash.
This is how we can do that. Delete is an inbuilt function of Perl. Here, we will see an example of assigning a hash to an array. Whenever you print a hash or when you store hash into an array.
The order may always differ. It's not the same always. We can assign only keys or values of a hash to an array. To remove all the entries in the hash, we can directly assign the hash to null. So, what are conditional statements? Conditional statements are those, where you actually check for some circumstances to be satisfied in your code.
Think about an example, you are buying some fruits, and you don't like the price to be more than bucks. So, the rule here is bucks. Perl supports two types of conditional statements; they are if and unless. Perl If If code block will be executed, when the condition is true. Is it a limitation? No, you can also control various conditions using if… elsif … else. There could be situations where both if and elsif code blocks will be failed. In this scenario, the else code block will be executed.
You can actually eliminate the else code check if you don't like to include. Perl Nested If In this case, you can use if code block in one more if code block. Perl Unless You have already got an idea what if does If the condition is true it will execute the code block. Unless is opposite to if, unless code block will be executed if the condition is false. Inside 1st else block — The value is 5 Guess what will be the output. You are right!!!!!. The output will be the print statement of the else block.
Because of the condition in unless code block is true, remember unless block will be executed only if the condition is false. Inside unless Block Perl Loops - Control Structures Perl supports control structures similar to other programming languages. Perl supports four types of control structures for, foreach, while and until. We use these statements to, repeatedly execute some code. Appendix G: Appendix H: Appendix J: Support, Errata and P2P.
Code Examples tar. Buy This Book: Perl is a well-established programming language that has been developed through the time and effort of countless free software programmers into an immensely powerful tool that can be used on pratically every operating system in the world. Perl started out as the "Swiss army knife" of computer languages and was used primarily by system administrators, but over time it's grown into an immensely robust language used by web-developers and programmers worldwide.
It's also a great way to learn programming techniques and develop your own style of coding. We'll take you from installation, through the core language elements - regular expressions, references, modules and the like - and onto basic applied techniques. You'll see how to access and work with databases in Perl, write CGI scripts to generate web pages and beyond. Coverage is bang up to date, using Perl 5. This book is for those new to programming who want to learn with Perl. You will find it easier if you already have some basic programming experience, but the material covered can be mastered by anyone with just a text editor, some common sense and a computer.
If you want to learn how to program with Perl and then find out how far you can go with it, this is the book for you. An excellent companion on any programming course. Toggle navigation. About 5.