PERL is Practical Extraction and Reporting language, which is a high level programming language written by Larry Wall. The more recent expansion is Pathologically Eclectic Rubbish Lister .
PERL is a free open source language.
It is simple to learn as its syntax is similar to C
It supports OOP – Object oriented programming like C++
Unlike C/ C++ it is a lot more flexible in usage
When do we use PERL for Programming:
Generally PERL is used to develop web based applications even though libraries are available to program web server applications, database interfaces and networking components. Example: The popular e-commerce site www.amazon.com was developed with PERL.
Advantages of programming in Perl
As mentioned above, PERL
-is easier to understand due to its simple syntax
-is easier to use due to its flexibility
-is easily readable
If the project requires OOP programming but requires faster execution
If the application to be developed is web based, Perl provides a lot of flexibility in programming such applications and is most popularly used.
Cost – As PERL is free, we can save on the cost of acquiring license for the programming language.
If the deadline is near, we can use CPAN, the Comprehensive Perl Archive Network, which is one of the largest repositories of free code in the world. If you need a particular type of functionality, chances are there are several options on the CPAN, and there are no fees or ongoing costs for using it.
modularize code and include them where required using the “use” command
use subroutines or functions to segregate operations thereby making the code more readable
use objects to create programs wherever possible which greatly promotes code reuse
include appropriate comments as and when required
eliminate any dereferencing operator
Warnings are one of the most basic ways in which you can get Perl to check the quality of the code that you have produced. Mandatory warnings highlight problems in the lexical analysis stage. Optional warnings highlight cases of possible anomaly.
The traditional way of enabling warnings was to use the -w argument on the
perl -w myscript.pl
You can also supply the option within the "shebang" line:
You can also mention use warnings with all, deprecated and unsafe options.
Eg: use warnings 'all';
Use and Require
Both the Use and Require statements are used while importing modules.
A require statement imports functions only within their packages. The use statement imports functions with a global scope so that their functions and objects can be accessed directly.
Eg. Require module;
Var = module::method(); //method called with the module reference
Eg: use module;
Var = method(); //method can be called directly
-Use statements are interpreted and are executed during the parsing whereas the require statements are executed during run time thereby supporting dynamic selection of modules.
My and Local
A variable declared with the My statement is scoped within the current block. The variable and its value goes out of scope outside the block whereas a local statement is used to temporarily assign a value to the global variable inside the block. The variable used with local statement still has global accessibility but the value lasts only as long as the control is inside the block.
For and Foreach
The for statement has an initialization, condition check and increment expressions in its body and is used for general iterations performing operations involving a loop. The foreach statement is particularly used to iterate through arrays and runs for the length of the array.
Exec and System
Exec command is used to execute a system command directly which does not return to the calling script unless if the command specified does not exist and System command is used to run a subcommand as part of a Perl script.
i.e The exec command stops the execution of the current process and starts the execution of the new process and does not return back to the stopped process. But the system command, holds the execution of the current process, forks a new process and continues with the execution of the command specified and returns back to the process on hold to continue execution.