Вы находитесь на странице: 1из 6

PERL5LIB=D:\oracle\product\10.2.0\db_1\perl\5.8.

3\lib\MSWin32-x86;D:\oracle\prod
uct\10.2.0\db_1\perl\5.8.3\lib;D:\oracle\product\10.2.0\db_1\perl\5.8.3\lib\MSWi
n32-x86;D:\oracle\product\10.2.0\db_1\perl\site\5.8.3;D:\oracle\product\10.2.0\d
b_1\perl\site\5.8.3\lib;D:\oracle\product\10.2.0\db_1\sysman\admin\scripts;
blrtng
perldoc perlfunc --- lists all the built in functions
perldoc perl
$\="\n";
$,=" ";
-------------------------------------------------------------$\="\n"; #OUTPUT record separator. This can be used instead of \n in every print
statement
$/="a";
# Input record separator
$,=" ;" # Field separator. This can be used instead of saparating fields in eve
ry print statement
-- perl one liners
perl -e "print 'Hi'; "
--- Types of variables
1. Scalar
- which hold single value. should begin with $.
ex: $variable_name=value; $a=10; $str='abc';
2. Array
3. Associate array
4. File handle
5. type glob
-- Types of operators:
Arithmatic :+ - * / % ** ++ -- += -= /= %=
Numaric Relactional op : > >= < <= == != <=>
String relational op: gt ge lt le eg ne cmp
x repeative operator
print "hi" x 5;
$x=10;
print $x x 3;
.. range operators
print 1..10;
print a..h;
print Aa..zZ;
-- standard input file handle
STDIN # while reading from a file handle use <>
$x=<STDIN>;
print $x;
print "done";
chop $x; -- chop deletes last char of a string it could be anything
chomp $x
-- deletes input separator of a string
$/="a";
$x=<STDIN>;
chop $x;
print $x;
print "done";
-- false values : "", undef, 0, 0.0, "0"

if (cond)
{
stat
}
else
{
stat
}
----- loop control statements
next -- similar to continue
last -- similar to bread
goto latbalename
for ($i=1;$<=10;$i++)
{
next if $i%2==0;
print $i;
}
$1=1
{
last if $i>10;
print $i;
$i++;
redo;
}
foreach $v ($x, $y,$z)
{
$v++;
print $v;
}
foreach ($x, $y,$z)
{
$_++;
print $_;
}
-- To list all pre defined variable values:
perl -d
crlt +c
V
--- To execute OS commands
1.
$x=`dir';
print $x;
print `dir`;
`dir';
2.

system("notepad.exe);
3.
exec("notepad"); -- terminates prog after closing notepad
-- Arrays
Ordered collection of scalar items
size cannot be fixed
it supports only single dimention
index no. starts from 0
@ sympbol is used to define array.
@arrayname=(list of values);
@cities=("BLR',"HYD","MUM");
@a1=("abc",34,64.26,'def');
@a2=qw(abc 34 64.26 def);
print "@a2"
print @a1;
print $a1[1];
print @cities[0,4,2];
print @cities[1..3];
@cities[0,3]=@cities[3,0];
-- to find the size of an array
$size=@cities;
print scalar(@cities);
print $#cities; -- holds last index no. of that array
@ARGV -- holds command line argument
$0
-- contains the name of the script
$$
-- process id no. of the scipt.
$ARGV
$_
ctrl + z
ctrl + d
@a1=<STDIN>;
print @a1;
chomp(@a1=<STDIN>); #to remove new lines
-- Array functions
1. push
syntax: push arrayname,[list of values]
-- returns the new size of the array;
push @a1,1,2,3;
print push @a1,a,b,c;
print push @a1;

print @a1;
2. pop
deltes the last element of the array
sys; pop [arrayname];
returns the element deleted;
pop @a1;
print pop @a1;
print @a1;
print @ARGV;
pop;
print @ARGV:
-- Logical operators
&& || !
and or not
$host=shift||"abc.com";
print $host;
---- splice:
delete or insert anywhere in an array
syntax: spfile(array,offset,no. of lement to be delte,list of va
lues to be iserted)
@a1=(12,13,14,15,16);
splice @a1,2,1,'a','b','c';
print @a1;
splice @a1,1,0,'f','s';
print @a1;
splice @a1,2;
print @a1;
splice @a1;
print @a1;
print defined (@a1); # to check array exits or not.
-- delete
one or more element deleted
other than the last ele
@ch=(a..g);
delete @a1;
-- sort
syn: sort array;
-- by default asc order
-- returns new sorted array
@a=qw(abc xyx ijk pqr def);
@new= sort @a1;
print @a;
print @new;

Numerical order:
print sort {$a<=>$b} @a2;
print sort {$b<=>$a} @a2;
@a3=(12,34,"abc","xyx","we",1,,156, 33);
print @a3;
print sort @a3;
print sort {$a<=>$b || $a cmp $b} @a3;
-- revers
syn: revers array
returns an array in reversed array
print revers @a1;
-- split
syn: split pattern,string
returns array of values
$str="perl session 4 days";
@w= split "e", $str;
print @w;
-- join
syn:join pattertn, array
return a string
$s=join "e",@w;
print $s;
-- grep
syn: grep pattern/condition, array
this fun returns all the elements that satisfies te cond
@g=(1,5,34,7,12,8,22,82);
@dev2=grep($_%2==0,@g);
print @dev2;
@g2=grep(m/2$/,@g);
print @g2;
-- map
@m=map($_*2,@g);
print @m;
-- Associative array
it is also called as hash
it is an unordered collection of key value pairs
cannot fix the size
% is used for this
a key can have only one value
value can be undef but not the key name
syn: %hashname=(k1,v1,k2,v2,k3,v3,k4,v4,...);
syn: %hashname=(k1=>v1,k2,v2=>k3=>v3,k4=>v4,...);

%cities=("kar","BLR", "TN","ch","ap","hyd", "MH","MUM");


print %cities;
print $cities{"kar"};
print @cities{"kar","ap"};
-- there are 5 hash functions
1. kyes
@keynames=keys %cities;
2. Values
@listofvalues= values %cities;
3. exists
--returns tru if key name exists in a given hash
print exists $cities{"kar");
4. each
--returns one pair at a time or undef if called once mor
e than the total no. of pairs.
print each %cities;
print each %cities;
print each %cities;
print each %cities;
print each %cities;
while(@r= each %cities)
{
print @r;
}
5. delete
-- to deelte one or more pair
-- returns the value of the deleted key
print delete $cities{"kar"};
print delete @cities{"kar","MH"};
Built in hashes
%ENV
%SIG
%INC

Вам также может понравиться