Sei sulla pagina 1di 5

#! /usr/bin/perl or #! /usr/bin/perl -w if we want warnings..

print
print "File not existed";
print "$logic\n";
Variables:-
$fred=5;
$logic= $fred + 2;
$logic += 3;
$logic *= 5;
$logic= 'abc';
$concat = $logic."def";
$mail= abcd\@gmail.com;
$adc = "he ate $fred mea's \n";
$adc = "He ate".$fred . 'meals' ;
@lines=`ls -l`;
@array=(1,2,3,4,5);
@array=(abc,2.3333,3,4,5);
@array=(1 .. 5); 1,2,3,4,5
@array=(1,2..6,10,12); 1,2,3,4,5,6,10,12
Operator precedence
priority -->parantheses and left associativty.
Operators:
+ _ * /
++ -- **
&
|| &&
lt,le,gt,ge , <, <=, >,>=
==, !=

If control structure
if($fred gt 'rajesh'){
}else{
}

<STDIN>
$line= <STDIN> #It receives the input from keyword, till \n is entered.
@array= <STDIN> # USE CTRL+Z OR CTRL + D for eof. now array wil store all the li
nes from keybaord till it receives EOF from keybaord.
chomp opretator:-
chomp($line);
chomp(@array); removes all the newlines.
Chomp is a function. It returns the value, that number of characters it removed.
If it removes \n. then it returns 1.
if more \n are there, chomp removes only one and it returns that 1.
if no \n, then it returns 0.

Chop operator:
It removed any last one or any one trailing character in the scalar variable.
$abc="tataconsultancy serrvices";
chop($abc); #$abc="tataconsultancy serrvice";
While loop:-
$fred=20;
while($fred >= 10)
{
$fred -=1;
}
while(<SRCE>)
{
print CONFIG ($_);
}
while(<>)
{
print CONFIG ($_);
}
POP and pUSH
@array= (5 .. 9);
$Last= pop(@array); 9 is removed from array and moved into $last.
pop @array; 8 is removed.
push (@array,9); again 9 is added into the array.

Shift and unshift operators:-


These moves the start of the array unlike pop/push in the start of the array.
@array= 2 .. 4;
$first= Shift (@array); 2 is moved to $first. in array its is removed.
unshift(@array, 1 .. 2); 1,2 is added into the array at the starting.

Easy to swap:
($fred,$abc)=($abc,$fred);
Foreach control structure:-
foreach $arraymember( 1 .. 10)
{
Print "$arraymember\n";
}
foreach $var (@array)
{
print "$var\n";
}

Default variable $_ in Foreach loop


foreach (1 .. 10)
{
print "$_\n";
}
reverse operator:-
It reverses the order in array
@array= (1 .. 10);
@array=reverse(@array); 10,9,8,..1
The sort Operator:-
It sorts based on Asciibetical order.. In this numbers come first, then capital
letters,
lower cases letters.
@array= (10,8,15,25);
@array=sort(@array); numbers in descending order. 25,15,10,8
Subroutines:-
#! /usr/bin/perl
&calling;
sub calling{
print "I am in a subroutine\n";

}
whatever the calculation performed at last in the subroutine is the return value
.
Subroutine with arguments:0
$return value= &calling (10,25);

sub calling {
print $_[0];
print $_[1];

}
# All the passed arguments to the subroutines to stored in the variables $_[0] .
.. $_[n].
This list is called parameter list or argument list.
And also all the arguments or parameters are stored in the array variable called
@_
So @_ varible is local to the subroutine.
If the same subroutine called recursively also, seperate @_ variables are create
d.
by default all the varibles in perl are global varibles.
Any variables in any subroutines can be accessed by any subroutine without passi
ng it.

MY is keyword makes the variable local to the subroutine.


Use strict pragma:p ragma is something hint to the compiler about the code.
Use strict pragma tells the compiler that its has to enforce the good rules to b
e followed in coding.
perl will insist that every new varible shall be declared as My lexical variable
.
The return operator:
the return operator is used in subroutine.
Hash:-its like a structure.
defining methods:-
$new {"key"} = {"value"};
$familyname{"key1"}={"value1"};
$familyname{"key2"}={"value2"};

Hash as whole:-
%familyname=("key1","value1","key2","value2");
%familyname=("key1" =>"value1","key2"=>"value2");
Moving:
$values1=$familyname{"key1"};
%hashvar=%familyname;
print %familyname;
Functions for Hash variable(keys,values,each,delete):-
@allkeys= keys %familyname;
@allvalues= values %familyname;
$allkeyscount= keys %familyname;
($key,$val)=each %familyname;
while(($key,$val)=each %familyname)
{
print $key $val;
}
foreach (sort (keys %familyname))
(
print $_;
)
Delete function is used to delete the particular key(and its value)
delete $familyname{"key1"};
Regular expressions:
meta characters:
. * + ?
[], ^ () =~ m//ig s//
\d \w
Split operator and join operator
@field =split/:/,"abc:def:ght" or @field =split( /:/,$line);
Join fucntion puts the glue string in between pieces and returns the resulting s
tring.
$x = join ":", a,b,c,d --> a:b:c:d
FILE Operations.. open(read,write and append), close,
open CONFIG,"filename.txt"; or open (CONFIG, "filename.txt");
if the file is in different directory then
open (CONFIG, "/a/b/c/filename.txt");
open CONFIG,"<filename.txt"
open CONFIG,">filename.txt"
open CONFIG,">>filename.txt";
close CONFIG;
open(CONFIG,"<abc.txt") || die("File open is failed\n");

Potrebbero piacerti anche