title | description | created | updated |
---|---|---|---|
Perl |
Perl Programming language cheatsheet contains useful code syntax with examples which is handy while coding. |
2020-05-02 |
2020-05-02 |
Perl(Practical Extraction and Report Language) is especially designed for text processing by Larry Wall. Perl is a high level programming language and hence one can easily undertand Perl as the programs are in simple english.
print "Hello World!!"; # to display "Hello World!!"
print
-- to print the data given#
-- single line comments
Perl has three basic data types:
- Scalars
- Arrays
- Hashes
$var-name =value; #scalar-variable
@arr-name = (values); #Array-variables
%hashes = (key-value pairs); # Hash-variables
In Perl, there is no need to explicitly declare variables to reserve memory space. When you assign a value to a variable, declaration happens automatically.
Operator type | Description |
---|---|
Arithmetic Operator | + , - , * , / , %, ** |
comparision Operator | < , > , <= , >=, != , == |
Bitwise Operator | & , ^ , |, ~, <<, >> |
Logical Operator | && , ||, ! |
Assignment Operator | = , += , -= , *= , /= , %=, <<=, >>=, &=, ^=, |= |
Quote Operator | q{ }, qq{ }, qx{ } |
Below are some of the useful string functions in Perl.
String Function | Description |
---|---|
length | This function is used to return the number of characters of a given string |
substr | This method is used to modify a substring in a string |
index | Searches for a substring in the given string and returns the position of the first occurrence of the substring if found |
rindex | Similar to index but searches for a substring from right to left |
reverse | This function is used to reverse a string |
lc | This function is used to convert the specified string to lowercase |
uc | This function is used to convert the specified string to uppercase |
crypt | This function is used to encrypt password |
q/string/ | used to create single-quoted strings |
qq/string/ | used to create double-quoted strings |
chr | to return ASCII or UNICODE character of a number |
hex | used to convert a hexadecimal string to it's equivalent decimal value |
oct | used to convert an octal number to it's equivalent decimal value |
ord | returns the ASCII value of the first character of a string |
sprintf | Formats string provided by the user and returns the formatted string to be used with print() |
List is a series of scalar values separated by commas and enclosed in round brackets. Lists are immutable.
(); # empty list
(1,2,3,4,5); # integer list
("Hello", "World"); #string list
("happy", 16) # list with different types of data
Hash is an unordered set of key/value pairs. They are preceded by %
.
%nationalGame = (Australia => 'Cricket',
Japan => 'Wrestling',
NewZealand => 'Rugby',
USA => 'Baseball',
England => 'Cricket');
Array is a variable which gives dynamic storage for a list.
my @fruits = qw(Apple Orange Grapes Kiwi Watermelon Banana);
Array Operations | Description |
---|---|
$count | returns the number of elements in the array |
$# | returns the highest index of an array |
push() | appends one or more elements to the end of an array |
unshift() | adds one or more elements to the front of the array |
pop() | removes the last element from the end of an array |
sort() | used to sort an array in alphabetical or numerical order. |
if(conditional-expression)
{
#code
}
if(conditional-expression)
{
#code
} else {
#code
}
if(conditional-expression-1)
{
#code
} elsif(conditional-expression-2) {
#code
} elsif(conditional-expression-3) {
#code
}
....
else {
#code
}
Nested-Ifs represents if block within another if block.
if(conditional-expression-1) {
#code
if(conditional-expression-2) {
#code
if(conditional-expression-3) {
#code
}
}
}
Unless is similar to If and is equivalent to if-not
.
statement unless(condition-expression);
#or
unless(condition-expression){
#code
}
unless(condition-expression){
# unless code
}else{
# else code
}
Given is similar to Switch in other programming languages.
given(conditional-expression){
when(value1){#code}
when(value2){#code}
when(value3){#code}
...
}
for(Initialization; Condition; Increment/decrement){
#code
}
#or
for (range){
#code
}
while(condition){
#code
}
do{
#code
} while(condition);
until(conditional-expression){
# code
}
do{
# code
}until(condition-expression)
package className;
$object = new className( Attributes);
sub methodName{
#code
}
sub SubName [PROTOTYPES] [ATTRIBUTES] { # defining a sub-routine
#code
}
&SubName; # calling a sub-routine
#or
SubName(argument-list); # calling a sub-routine, argument-list is optional
sum(10,20);
sub sum() {
$sum = 0;
foreach $item (@_) {
$sum += $item;
}
print "sum is: $sum"
}
open(filehandle,mode,filename)
Mode | Symbol |
---|---|
read | < |
write | > |
append | >> |
open(FH, '<', 'c:\sample.txt');
close(FH);
File test Operator | Description |
---|---|
-r | checks if the file is readable |
-w | checks if the file is writable |
-x | checks if the file is executable |
-o | checks if the file is owned by effective uid. |
-T | checks if the file is an ASCII text file. |
-B | checks if the file is a binary file. |
-e | checks if the file exists. |
-z | checks if the file is empty. |
-s | checks if the file has nonzero size. |
-f | checks if the file is a plain file. |
-d | checks if the file is a directory. |
-l | checks if the file is a symbolic link. |
-p | checks if the file is a named pipe (FIFO. |
-S | checks if the file is a socket. |
-b | checks if the file is a block special file. |
-c | checks if the file is a character special file. |