This reference has been prepared for beginners to help them understand the basic to advanced concepts related to Perl Scripting languages. Prerequisites. Perl Tutorial in PDF - Learn Perl in simple and easy steps starting from basic to advanced concepts with examples including Introduction, Environment, Syntax. preckalohotchning.ml free online Perl books: Beginning Perl. Coverage is bang up to date, using Perl in all our examples and there's a handy reference section in the.
|Language:||English, Indonesian, Dutch|
|Genre:||Health & Fitness|
|ePub File Size:||28.84 MB|
|PDF File Size:||9.48 MB|
|Distribution:||Free* [*Sign up for free]|
q The First Line of Your Perl Program: How Comments Work In your first week of learning Perl, you'll learn enough of the basics of Perl to write many useful. get pretty far with just the basics, and that's what this document is about. Perl is a free, open source programming language created by Larry Wall. Perl aims for. Perl is fast, especially at common tasks in biology: file manipulation and pattern matching. ▫ Good at manipulating large data sets or performing the same task.
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?
Introduction to AutoCAD Plant 3D 2016 - All IT eBooks
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 downloading 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. For loop Perl For code block will execute till the condition is satisfied. Let's take an example of how to Perl loop an array.
The array index 0 value is 1 The array index 1 value is 2 The array index 2 value is 3 The array index 3 value is 4 The array index 4 value is 5 The array index 5 value is 6 The array index 6 value is 7 The array index 7 value is 8 The array index 8 value is 9 The array index 9 value is 10 Here, in for expression, there are many statements included. There is a meaning for each of them. Let's take the same example with foreach perl.
The value is 1 The value is 2 The value is 3 The value is 4 The value is 5 The value is 6 The value is 7 The value is 8 The value is 9 The value is 10 This looks good for accessing arrays. How about Hashes, how can we obtain hash keys and values using foreach? We can use foreach to access keys and values of the hash by looping it. Keys is an inbuilt function of Perl where we can quickly access the keys of the hash.
A fascinating conversation...
How about values? We can use values function for accessing values of the hash. The code block will exit only if the condition is false. Let's take an example for Perl While loop. Here is a problem, which will require input from the user and will not exit until the number provided as '7'. Guess a Number Between 1 and 10 9 Guess a Number Between 1 and 10 5 Guess a Number Between 1 and 10 7 You guessed the lucky number 7 In the above example, the while condition will not be true if we enter input other than '7'.
If you see how while works here, the code block will execute only if the condition in a while is true. Perl do-while Do while loop will execute at least once even if the condition in the while section is false. Let's take the same example by using do while. Here, the code block will execute only if the condition in until block is false. Let's take the same example which we used in case of a while.
Here is a problem, which will require input from the user and will not exit until the name provided as other than 'sai'. Enter any name sai Perl do-until: Do until can be used only when we need a condition to be false, and it should be executed at-least once.
Enter any name Howard Enter any name Sheldon Enter any name sai Execute while, do-while, until and do-until example codes to see the difference.
Perl Operator What is Operator? Operators in computer language indicate an action that can be performed on some set of variables or values which computer can understand.
Perl has incorporated most of the Operators from C language. Perl has many operators compared with other programming languages. Operators are categorized as Arithmetic, Logical, relational and assignment operators. Arithmetic Operators: Arithmetic operators are those which can be used to perform some basic mathematic operations. These Arithmetic operators are binary operators where we need two arguments to perform a basic operation. We can also use unary operators for other basic operations; you can see the difference in examples below.
Add of 10 and 2 is 12 Sub of 10 and 2 is 8 Mul of 10 and 2 is 20 Div of 10 and 2 is 5 Exp of 10 and 2 is Mod of 10 and 2 is 0 Assignment Operators: Assignment operators simply assign values to variables, but there is one more thing which we need to remember here, assignment operators will also perform arithmetic operations and assign the new value to the same variable on which the operation is performed.
Perl uses logical operators to compare numbers and strings. Most of the time logical operators are used in Conditional Statements. True -- equal 5 and 5 True -- not equal 6 and 7 True -- 7 greater than 6 True -- 6 less than 7 True -- 6 less than 7 True -- 7 greater than 6 You can see examples of logical operators in later sections.
Special variables in Perl are those who have some predefined meaning. These variables denoted with either real Name or Punctuation symbols. We have a special variable for all the Perl supported Variables like scalar special variables, Array special variables, hash special variables.
Most of the special variables that we use are of scalars. When we want to use the special variable with its name, then we have to load a Perl module 'use English,' to explicitly say Perl interpreter that we are going to use special variables using its Name.
By default value of this is set to 0, we can change the value of this variable. Holds the format of the currently selected output by default the file handle name. Status code: Pipe and system call. The return status of the command executed. Holds the values of the pattern matched in order. Array, a substring. In numeric context, prints the error number.
Beginning Perl (free)
In string context, print the error. Warning switch. Array Special Variables: Variable Description INC Holds a list of paths, where Perl library modules or scripts can be looked into while executing the current script. This INC is used by use and require statements to look into those paths for library modules. ARGV Stores the command line arguments passed. F This is the array into which the input lines are stored when auto split —a command line switch is used. Hash Special Variables: Used by do, use and require.
Perl regular expression is strong enough in matching the string patterns within a statements or group of statements. Regular expressions are mostly used in text parsing, pattern matching and much more based on the requirement. Match 0 or 1 character Can be used to match alternative patterns Used to store the pattern matched  Set of characters can be passed. Specifically used for numeric and alphabets.
Let's see few examples. Consider a situation where user provides some input during script execution, and we want to check whether the user entered some name as input or not. We have to write a regular expression syntax to extract your name and print the same.
Let us see how exactly we can construct a Regex. Consider an example of a string of multiple words and digits and special symbols like this "Hello everyone this is my number: Perl Match Operators The match operators are used to match a string within some statement or in a variable.
The pattern can be anywhere in the variable. Perl Substitution Operator This operator can be used for searching and replacing any character with either null or some other character. We can actually use any pattern matching string as we did earlier in this substitution operator as well. Here we used 'gi', g-globally, i-ignore case. Perl Translation Operator This is similar to Substitution, but it does not use any perl regular expressions, rather we can directly pass the value or a word which we want to replace.
Perl main advantage is in file parsing and handling the files. There are many inbuilt functions and operators used during file handling in Perl. In this tutorial, you will learn- Perl Open file We can open a file using open function available in Perl. Is it for reading or writing? Consider we have a perl file with name file. We need to open this file and print the same. Now, we will write a program to create and write data to a perl file.
The above way will always try to create a file named test.
We will see few more examples and other functions which help in understanding more about files. This method is used to position the file pointer to a specific location by specifying the bytes followed by either start of the file pointer or end of the file pointer. Zero will set it from the beginning of the file.
Let input. We can also handle directories through which we can handle multiple files. We can use the opendir and readdir methods.
Subroutines are similar to functions in other programming languages. We have already used some built-in functions like print, chomp, chop, etc. We can write our own subroutines in Perl. These subroutines can be written anywhere in the program; it is preferable to place the subroutines either at the beginning or at the end of the code. Most of the reusable code requires parameters to be passed to the subroutine. Here, we will learn how we can pass arguments to the subroutine.
Main functionality of subroutines is to do some task and return the result of the reusable code. We can return a value from subroutine using the return keyword. We can also pass hashes and array directly to the subroutine. Inside Sub-routine2b1aAfter Sub-routine call2b1a Perl Format Perl has a mechanism using which we can generate reports.
Using this feature, we can make reports exactly the way we want while printing on the Output screen or in a file.
A simple format can be written using printf or sprintf functions available in Perl. The same can be used for sprintf.
Using printf and sprintf, we can obtain most of the Perl formats. In the case of reports, it will be difficult to implement. Example of a Report: This can be achieved by using the perl printf and perl sprintf.
It can effectively be implemented using format. A format can be declared in the below manner. Here, we will be using a particular method write to print the data onto output screen or into the file. Assumes as a comment if single provided. Execute the code to see the output. We can change this to file handler which we are using to write data into the file.
This will output the format to the file which we are working on. Perl Coding Standards Each programmer will have his own sense of writing the code using certain standards; these standards should be familiar enough that other programmer can understand and support the code properly.
Writing code is simple and easy. The problem arises when it needs to be maintained at later stages. Proper guidelines and coding standards need to be followed while writing the code.
Perl also defines certain standards that will be useful for programmers to write code. It is advisable to load 'strict' and 'warnings module' while writing the code. Each of these modules has its own importance. 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. 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. 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: cpan App::cpanminus cpan App::cpanminus Make sure you have internet connection while perl modules are being downloaded and installed.
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. Scalars can be read from devices, and we can use it to our programs.Using this you can declare any variable which is specific within the block. It's alternative to the decimal system. We can change this to file handler which we are using to write data into the file. But don't worry- just a few. It stands for Comprehensive Perl Archive Network and contains a vast amount of Perl modules for you to use.
Mostly we will be using. You need to manually install those. Most of the time logical operators are used in Conditional Statements. Appendix A: