PERL is an acronym for Practical Extraction and Report Language. Perl is a general purpose programming language designed to be used for text processing but is also used for system administration, web development, network programming, GUI development etc. It supports both procedural and object oriented programming and a wide collection of third party modules are available.
Perl is a programming language which can be used for a large variety of tasks. A typical simple use of Perl would be for extracting information from a text file and printing out a report or for converting a text file into another form. But Perl provides a large number of tools for quite complicated problems, including systems programming.
Perl was introduced by Larry Wall circa 1987 as a general purpose Unix scripting language to make his programming work simpler.Perl is implemented as an interpreted language. Thus, the execution of a Perl script tends to use more CPU time than a corresponding C program, for instance. As computing became faster now a days writing something in Perl instead of C tends to save time.
To check whether PERL installed on your system already,go to command prompt and type perl -v. If Perl is installed correctly, it will display the version information.
Comments in PERL -
The symbol # indicates a comment in PERL. Comments help in understanding and debugging code, so it's always a best practice to put useful comment in any programming language while coding/scripting.Good comments are short, but instructive. They tell you things that aren't clear from reading the code.
A code without proper comment is considered as bad comment, check the following code : Simply by looking at this you wont get anything from this.
for $i (@q)
{
my ($j) = fix($i);
Similarly a code with improper comment also considered as bad code, check this one -
for $i (@q)
{
# @q is list from last sub
my
($j) = fix($i); # Gotta fix $j...
transmit($j); # ...and then it goes over the wire
}
Following is considered as good code snippet as it has proper comment. Simply by looking at this we can understand what exactly this part of script doing.
# Now that we've got prices from database, let's send them to the buyer
for $i (@q) {
my ($j) = fix($i); # Add local taxes, perform currency exchange
transmit($j);
}
3 variable types: Scalars, Arrays, and Hashes.
Scalars -- > A scalar represents a single value. Scalar values can be strings, integers or floating point numbers, and Perl will automatically convert between them as required. There is no need to pre-declare variable types.
my $animal = “camel”;
my $answer = 42;
Arrays -- > An array represents a list of values. Arrays are zero-indexed. Here’s how you get at elements in an array. The special variable $#array tells you the index of the last element of an array.
my @animals = (“camel”, “llama”, “owl”);
my @mixed = (“camel”, 42, 1.23);
print $mixed[$#mixed]; # last element, prints 1.23
Hashes -- > A hash represents a set of key/value pairs. You can use whitespace and the => operator to lay them out more nicely. You can get at lists of keys and values with keys() and values().
my %fruit_color = (“apple”, “red”, “banana”, “yellow”);
Conditionals and looping :
The following looping constructs are available in Perl.
- IF
- WHILE
- FOR
- FOREACH
FOREACH is widely used construct to loop through lists without explicitly defining multiple variables.
Built in Operators:
Arithmetic
+ addition, - subtraction, * multiplication, / division
Numeric comparison
== equality, != inequality, < less than, > greater than, <= less than or equal, >= greater than or equal
String comparison
eq equality, ne inequality, lt less than, gt greater than, le less than or equal, ge greater than or equal
Boolean Logic
&& and, || or, ! not
Miscellaneous
= assignment, . string concatenation, x string multiplication, .. range operator (creates a list of numbers)
Apart from these basic features, simple solutions can be arrived at for complex problems using data structures in Perl.In this post, we've only scratched the surface of what Perl can do. PERL can do much more than this, you can get more information on PERL from here.
No comments:
Post a Comment