0% found this document useful (0 votes)
37 views

Perl Programming

perl
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Perl Programming

perl
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Perl Programming

Author: Tigran Khazhakyan


Introduction
to Perl

Tigran Khazhakyan 2
Basic Terms

Computer Program

Source code Executable

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

print Hello World\n; % ./helloworld.pl

Output Explanation

#!/usr/bin/perl - look up the path of perl


Hello World interpreter

Perl has single-line comments start with #


print receives a string argument and
outputs to the terminal

Tigran Khazhakyan 6
Variables

Variables

That store That store


single value multiple value

Arrays
scalars
Hashes

Tigran Khazhakyan 7
Using Scalars

#!/usr/bin/perl

$name = Perl;
Scalars
$age = 25;

print $name is $age years old!, \n;

Tigran Khazhakyan 8
Escape Characters: Backslash

#!/usr/bin/perl

$name = students;
$money= 1000;

print $name earn \$money per month\n;

Tigran Khazhakyan 9
Operations on Scalars: Arithmetic
#!/usr/bin/perl

# file name arithmetics.pl

$str = String equals to 0;


$operand1 = 11;
$operand2 = 55;

print Value of string = $str\n;


print $operand1 + $operand2, \n;
print $operand1 - $operand2, \n;
print $operand1 * $operand2, \n;
print $operand1 / $operand2, \n;
print $operand1 % $operand2, \n;

Tigran Khazhakyan 10
Operations on Scalars: Increments and Decrements

#!/usr/bin/perl

$numericVariable = 11;

print ++$numericVariable, \n;


$numericVariable = 11;
print $numericVariable++, \n;
print $numericVariable;
print --$numericVariable, \n;
$numericVariable = 11;
print $numericVariable--, \n;

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

@fruits = (apple, banana, pear);


$number = @fruits;
$new_array[0] = 5;
$new_array[1] = 6;

print $fruits*0+ is a fruit!\n;


print There are $number fruits\n;
print @new_array, \n;

Tigran Khazhakyan 14
Using Arrays (Lists): push/pop

#!/usr/bin/perl

@fruits = (apple, banana, pear);


$last_el = pop @fruits;
print @fruits;

push @fruits, pineapple;


print @fruits;

Tigran Khazhakyan 15
Using Hashes

#!/usr/bin/perl

%prices = (Hot Dog => 300, Pizza => 2000);


$prices,Burger- = 1600;

print keys %prices;


print values %prices;
print array: @prices,Pizza, Burger-;

Tigran Khazhakyan 16
Perl Conditional Statements: IF/ELSE/ELSIF

#!/usr/bin/perl #!/usr/bin/perl

if(boolean statement) { if(boolean statement1) {


#code #code
} else { } elsif (boolean statement2)
#code {
} #code
} else (boolean statement3)
{
#code
}
Tigran Khazhakyan 17
Perl Conditional Statements: unless/else/elsif

#!/usr/bin/perl #!/usr/bin/perl

unless(boolean statement) { unless(boolean statement1) {


#code #code
} else { } elsif (boolean statement2) {
#code #code
} } else {
#code
}

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;

while ( $a < 20 ) { until ( $a < 20 ) {


print "Value of a: $a\n"; print "Value of a: $a\n";
$a = $a + 1; $a = $a + 1;
} }

Tigran Khazhakyan 20
Perl Loops: for

#!/usr/bin/perl

$a = 10;

for ( $a = 10; $a < 20; $a = $a + 1 ) {


print "value of a: $a\n";
}

Tigran Khazhakyan 21
Perl Loops: foreach

#!/usr/bin/perl

@list = (2, 20, 30, 40, 50);

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

1. Find the sum of all the multiples of 3 or 5 below 1000.

2. By considering the terms in the Fibonacci sequence


whose values do not exceed four million, find the sum of
the even-valued terms.

3. What is the largest prime factor of the number 14256

4. Find the sum of all the primes below 16000

Tigran Khazhakyan 25

You might also like