Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
Easy to swap:
($fred,$abc)=($abc,$fred);
Foreach control structure:-
foreach $arraymember( 1 .. 10)
{
Print "$arraymember\n";
}
foreach $var (@array)
{
print "$var\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.
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");