Perl Programming
Perl Programming
Tigran Khazhakyan 2
Basic Terms
Computer Program
Algorithm
Compiler Interpreter
Languages
Scripting Programming
Tigran Khazhakyan 3
Compiler vs Interpreter
Interpreter Compiler
Scans the entire program and
Translates program one statement at
translates it as a whole into machine
a time.
code.
It takes large amount of time to
It takes less amount of time to
analyze the source code but the
analyze the source code but the
overall execution time is
overall execution time is slower.
comparatively faster.
No intermediate object code is Generates intermediate object code
generated, hence are memory which further requires linking, hence
efficient. requires more memory.
Continues translating the program It generates the error message only
until the first error is met, in which after scanning the whole program.
case it stops. Hence debugging is Hence debugging is comparatively
easy. hard.
Programming language like Python, Programming language like C, C++ use
Ruby use interpreters. compilers.
Tigran Khazhakyan 4
The Very Beginning
Start
printed Hello
World
End
Tigran Khazhakyan 5
The Very Beginning
Run
Code
% chmod +x helloworld.pl
#!/usr/bin/perl % perl helloworld.pl
# file name helloworld.pl or
Output Explanation
Tigran Khazhakyan 6
Variables
Variables
Arrays
scalars
Hashes
Tigran Khazhakyan 7
Using Scalars
#!/usr/bin/perl
$name = Perl;
Scalars
$age = 25;
Tigran Khazhakyan 8
Escape Characters: Backslash
#!/usr/bin/perl
$name = students;
$money= 1000;
Tigran Khazhakyan 9
Operations on Scalars: Arithmetic
#!/usr/bin/perl
Tigran Khazhakyan 10
Operations on Scalars: Increments and Decrements
#!/usr/bin/perl
$numericVariable = 11;
Tigran Khazhakyan 11
Operations on Scalars: Strings
#!/usr/bin/perl
# file name operations.pl
$a = Hello;
$b = World;
print $a . $b;
print $a x 5;
Tigran Khazhakyan 12
Operations on Scalars: Assignments
#!/usr/bin/perl
# file name operations.pl
$a = 5;
$b = 4;
$a = $b; # Assign $b to $a
$a += $b; # Add $b to $a
$a -= $b; # Subtract $b from $a
$a .= $b; # Append $b onto $a
Tigran Khazhakyan 13
Using Arrays (Lists)
#!/usr/bin/perl
Tigran Khazhakyan 14
Using Arrays (Lists): push/pop
#!/usr/bin/perl
Tigran Khazhakyan 15
Using Hashes
#!/usr/bin/perl
Tigran Khazhakyan 16
Perl Conditional Statements: IF/ELSE/ELSIF
#!/usr/bin/perl #!/usr/bin/perl
#!/usr/bin/perl #!/usr/bin/perl
Tigran Khazhakyan 18
Perl Conditional Statements: switch
#!/usr/bin/perl
use Switch;
switch (argument) {
case 1 { #code }
case a { #code }
else { #code }
}
Tigran Khazhakyan 19
Perl Loops: while/until
#!/usr/bin/perl #!/usr/bin/perl
$a = 10; $a = 10;
Tigran Khazhakyan 20
Perl Loops: for
#!/usr/bin/perl
$a = 10;
Tigran Khazhakyan 21
Perl Loops: foreach
#!/usr/bin/perl
foreach $a (@list) {
print "value of a: $a\n";
}
Tigran Khazhakyan 22
Perl Loops: do while
#!/usr/bin/perl
$a = 10;
do {
print "Value of a: $a\n";
$a = $a + 1;
} while ( $a < 20 );
Tigran Khazhakyan 23
Perl Subroutines: sub
#!/usr/bin/perl
$a = 10;
sub myFunc {
my $a = $_[0]; # @_
print "Value of a: $a\n";
}
&myFunc(10);
Tigran Khazhakyan 24
Problems
Tigran Khazhakyan 25