Perl stands for Practical Extraction and Reporting Language.
Features of Perl:
- Perl supports both procedural and object-oriented programming.
- Perl takes the best features from other languages, such as C, awk, sed, sh, and BASIC, among others.
- Perls database integration interface DBI supports third-party databases including Oracle, Sybase, Postgres, MySQL and others.
- Perl's CGI.pm module, part of Perl's standard distribution, makes handling HTML forms simple.
- Perl's mod_perl allows the Apache web server to embed a Perl interpreter.
Interpreter vs Compiler
- Interpreters translate the same statements within a loop over and over again. Compilers can produce much more efficient object code than interpreters thus making the compiled programs to run faster. Compilation requires analysis and the generation of machine code only once, whereas an interpreter may need to analyse and interpret the same program statements each time it meets them
- Interpreter takes single line as input unlike compilers which takes entire program as input.
- Interpreter language does not generate intermediate object code.
- It generates byte code which is then converted to Machine code, unlike compilers which generates machine dependent code.
What is "#!/usr/bin/perl" ?
It's called a shebang. On Unix based systems (OSX, Linux, etc...) that line indicates the path to the language interpreter when the script is run from the command line.
Data Types
Three special literals __FILE__, __LINE__, and __PACKAGE__ represent the current filename, line number, and package name at that point in your program.Array Functions
Consider my @array = (1,2,3,4);
pop @array; print @array; /*Removes the last element from array, o/p will be */ 1,2,3
push @array,4,5 ; print @array; /*Pushes values to array end, o/p will be */ 1,2,3,4,5
shift @array; print @array; /*Removes the first element of the array, o/p will be */ 2,3,4,5
unshift @array,1 ; print @array; /*Pushes list to start of the array, o/p will be */ 1,2,3,4,5
my @array2 = @array[1,4]; print @array2; /* slices the array to form another array with few array elements, o/p will be */ 2,5
my @arry2 = splice(@array,2,2,7,8); print @array2; print @array;/*@ARRAY designated by OFFSET and LENGTH, and replaces them with LIST, if specified. Finally it returns the elements removed from the array */ 3,4 ; 1,2,7,8;
my $var="ravi kumar"; @array = split(/'\s'/,$var); print @array; /*splits the variable into array based on the condition*/
@numbers = (@odd, @even); /*Merge Two Arrays */
Hash Functions
exists : "f you try to access a key/value pair from a hash that doesn't exist, you'll normally get the undefinedvalue,".
delete $data{'Ali'}; /*Delete Hash element */
Conditional statements:
Unless ( ) {}; /*Opposite to if condition, that the statement should be false, then execute the code inside the {}*/
conditional operator ? : which can be used to replace if...else statements. It has the following general form: Exp1 ? Exp2 : Exp3;
String comparision are done by "le" and numerical comaprision is done by "<"
References in Perl:
Hard Link vs Soft Link (Linux)
A hardlink can only work on the same filesystem, it is simply a different name for the same inode (files are internally referenced by inodes). A file will only be deleted from disk when the last link to its inode is gone (you rmd or unlinkd the last link). Hardlinks usually only work for files, not directories.A symlink (symbolic link) is a special file containing a path to another file. This path can be absolute or relative. symlinks can work across file systems, and can even point to different files, if you for example unplug an external hard drive and replace it with another one, which has a different file at the same path. A symlink can point to either files or directories.
Object Oriented Perl:
1. Procedural programs emphasize on Task, like splitting the tasks and using subroutines to define the task. But OOP, emphasizes on Data. Chunks of data called Objects can have various properties and can interact with each other in various ways.
What Is an Object?
A component that contains properties and methods needed to make a certain type of data useful.
Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle.
Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.
Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions: "What possible states can this object be in?" and "What possible behavior can this object perform?". Make sure to write down your observations. As you do, you'll notice that real-world objects vary in complexity; your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming.

Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.
Consider a bicycle, for example:

By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6.
Bundling code into individual software objects provides a number of benefits, including:
Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle.
Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.
Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions: "What possible states can this object be in?" and "What possible behavior can this object perform?". Make sure to write down your observations. As you do, you'll notice that real-world objects vary in complexity; your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming.

A software object.
Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.
Consider a bicycle, for example:

A bicycle modeled as a software object.
By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6.
Bundling code into individual software objects provides a number of benefits, including:
- Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
- Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
- Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.
- Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.
What Is a Class?
A class is the blueprint from which individual objects are created.
In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.
A class provides a set of methods which become associated with a particular kind of object.
A class also provides a specification of the attributes to be used as well. It’s effectively a blueprint, describing what the object is to look like and how it will act.
Distinction between class, object, Method
When a program needs an object of a particular type, it calls upon this blueprint along with some information about the initial state of the object (like the advertising to put on the front of the drink machine, or which drinks it has to start with). The blueprint (class) makes sure these initial values are sensible, manufactures the appropriate object, and returns it.
When we call a method on an object (such as give_change), the class definition is consulted again to ensure that’s a valid method for the object and has been called correctly. If it is, that method is invoked and does its thing. If it isn’t, then an error or exception is usually raised.
What is Inheritance
Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear). Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example,
Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike.What is polymorphism
Polymorphism
is the ability for objects to respond differently to the same message, depending upon what type of object they are.
Polymorphism becomes very useful when we have a group of related objects upon which we want to perform an operation, but those objects may need to react in different ways.
For example, an ElectricCar will need to react differently to a FossilFuelCar when asked to accelerate.
There are two distinct forms of polymorphism in object oriented programming;
interface and inheritance polymorphism.
Interface polymorphism is where two or more unrelated classes provide the same interface to certain methods. For example both sparrows and aeroplanes can fly. Although sparrows and aeroplanes fly in completely different ways, if we implement our classes in such a way that the methods have the same arguments and argument order then we have an example of interface polymorphism.
Inheritance polymorphism is where a child class inherits methods from an ancestor. Hence if the Machine class, implemented a turn_on method then the DrinksMachine class would inherit that method. If we were to call the turn_on method on a DrinksMachine object the DrinksMachine object would behave as if it were merely a Machine object for that method call.
What Is a Package?
A package is a namespace that organizes a set of related classes and interfaces.