Beruflich Dokumente
Kultur Dokumente
SWAROOP KULKARNI
swaroopk@tataelxsi.co.in
H & S GROUP
Day 1 Schedule
•Features of Perl
• Interpreted Language
Interpreted Vs Compiled
Interpreted Language
• Code you enter is saved in the same format.
• Executed immediately after parsing - performed by the interpreter
• Relative ease of programming and no linker is required
• Disadvantages include poor speed performance and that you do not
generate an executable
• Ex : Perl,Java,Basic
Compiled Language
• Compilers parse the instructions into machine code and store them in a
separate file
• Compiled programs generally run faster
• Ex: C, C++, VB
Course Requisites and Goals
\a Bell (beep)
\n Newline
\t Tab
\u Force next letter to uppercase
\l Force next letter to lowercase
\U Following entire string to UC.
\L Following entire string to LC.
String Operators
. Concatenates two strings $var = $var1.$var2;
x Repeates a string $var = $var1 x 3;
.= Concatenates and assigns $var .= $var1;
#!/usr/bin/perl
for($i=1; $i <= 10; $i = $i+1) {
$sqrt = $i**0.5;
print "square-root of $i is $sqrt\n"
}
Perl Statements
• Foreach Statement
foreach $my_var (@my_arr)
{ next if $my_var = 10;
print “$my_var \n”;
last if $my_var = 5;
}
Array length
$my_var = @my_array - gives the length of the array
($my_var) = @my_array – assigns the first value of
the array
Lists and Arrays
Array slicing
@my_slice = @my_array [1,2,4];
Writing to a File
print my_handle "This line goes to the file.\n";
close function
close(my_handle);
File Handling in Perl
File test operators
-d Is name a Directory?
-e Does name exists?
-r Is name a readable file?
-s Is name a non-empty file? Returns size of file.
-w Is name a writable file?
-x Is name an executable file?
-z Is name an empty file?
Ex: if (!(-e “file1”))
print(“File doesnot exists”);
File Handling in Perl
File rename
rename(“file1”, “file2”)
Example for Reading File with Error handling
$line = <INFILE>;
while ($line ne "") {
chop ($line);
print OUTFILE ("\U$line\E\n");
$line = <INFILE>;
}
Pattern Matching
• Regular Expression is a pattern that the string is matched
for.
• Match Operators
=~ !~
Ex : $sentence =~ /the/ $line !~ /the/
Program:
open(IN_FILE, "alice.txt");
@lines = <IN_FILE> ;
close(IN_FILE);
# searching the file content line by line:
foreach $line (@lines){
if ($line =~/the/){
print $line;
} # end of if
} # end of foreach
Pattern Matching
• Special characters
. Any single character except a newline /t.e/
^ The beginning of the line or string /^a/
$ The end of the line or string /n$/
* Zero or more of the last character /^a.*n$/
+ One or more of the last character /to+/
? Zero or one of the last character /to?/
Pattern Matching
Special Characters [ ]
[qjk] Either q or j or k
[^qjk] Neither q nor j nor k
[a-z] Anything from a to z inclusive
[^a-z] No lower case letters
[a-zA-Z] Any letter
[a-z]+ Any non-zero sequence of lower case letters
Ex: /\bt\we\b/
Examples
Environment variables
$ENV{env_variable}
Program Arguments
@ARGV contains the list of command line arguments
System commands
system(“cd .. ; perl myperl.pl”);
SUBROUTINES
sub mySubRoutine {
<do something >
}
# invoke the sub routine
&mySubRoutine;
Example to illustrate the Subroutine
sub fun1()
{ my $a=0;
print "before calling another function the value is : " . ($a);
&fun2($a);
print "\n After calling the second function the value is : " . ($a);
}
sub fun2()
{ my($v)= @_;
$v =12;
print "\n after the my variable is passed & modified : " . s $v;
}
&fun1();
Passing Arguments to Subroutine
BEGIN {
Print { “ this is first subroutine to be called”};
}
• Multiple BEGIN subroutines can be defined. These
subroutines are called in the order in which they appear in the
program.
Predefined Subroutines
END {
Print {“ this is the last statement to be executed” };
}