Programming languages
Spring 2019
Introduction to PERL language
Konapalli Bhargav Reddy
CID - 12
Topics
• Brief History
• Versions of Perl
• Program Structure
• Data Types in Perl
• Standard function and User-Defined function
• standard I/O, file I/O, stream I/O
• Encapsulation/Inheritance
• Demo
Brief History
• Perl stands for Practical Extraction and Report Language
• Perl is the natural outgrowth of a project started by Larry Wall in
1986
• Perl languages borrow features from other programming
languages including C, shell script (sh).
• Perl belongs to Web Software programming domains (Scripting).
• Perl is also a high-level, general-purpose, interpreted, dynamic
programming languages .
Versions of Perl language
• 1987-Dec Perl 1.0
• 1988-Jun Perl 2.0
• 1989-Oct Perl 3.0
• 1991-Mar Perl 4.0
• 1994-Oct Perl 5.0
• 2014-may Perl 5.20
https://www.perl.org/get.html
How to download Perl software
Application of Perl
• IMDB
• Amazon.com
• BBC
• Booking.com
• Duckduckgo
Program Structure
Program Structure
• Comment lines begin with: #
• File Naming Scheme
=> filename.pl (programs)
=> filename.pm (modules)
• Statements must end with semicolon
=> print("Hello world");
• Should call exit() function when finished
=> Exit value of zero means success
exit (0); # successful
=> Exit value non-zero means failure
exit (2); # failure
Program Structure
• Executing a Perl script
=> perl <file_name>.pl
• We generally start a Perl script with the "shebang“ line
#!/usr/bin/perl
#!/usr/local/bin/perl
#!/usr/local/bin/perl5
#!/usr/bin/perl –w
The –w token is not necessary on many systems, but when it is, it is,
so it's a good habit to get into.
Data Types in Perl
• Most data is basically either a string or a number, or a bunch of
strings or numbers.
• A number is just an Integer value. 1 + 18
$n = 1234; # decimal integer
$n = 0b1110011; # binary integer
$n = 01234; # octal integer
$n = 0x1234; # hexadecimal integer
$n = 12.34e-56; # exponential notation
$n = "-12.34e56"; # number specified as a string
$n = "1234"; # number specified as a string
Data Types in Perl
• A string is character data: “bhargav", "11-30-04".
=> Always write strings within quote marks, as above.
• The concept of the variable is fundamental in all programming
languages. A variable is essentially a placeholder for a value
=> x + 1 = 5, x is a variable
Single quotes Double quotes
my $name = ‘bunny';
my $time = "today";
print "Hello $name,nhow are you $time?n";
The output will be
Hello bunny,
how are you today?
my $name = ‘bunny';
my $time = "today";
print ‘Hello $name,nhow are you $time?n’;
The output will be
Hello $name,nhow are you $time?n
Data Types in Perl
• Three most important types of variables are the scalar, the array and the
hash
=> A scalar is a variable that holds a single value.
Scalar names begin with $
ex: $name = “bhargav";
=> An array is a variable that holds multiple values in series. Array names
begins with @
ex: @names = (“bhargav",“bunny");
=> A hash is a variable that holds pairs of data. Hash names begin with %
ex: %traits = ("firstname" => "bhargav", "lastname" => "reddy");
Sequence control in Perl
While for
$count = 10;
while ($count > 0) {
print "Countdown is: $countn";
$count--;
}
for ($count = 1 ; $count > 10 ; $count++) {
print "My count is: $countn":
}
Sequence control in Perl
until foreach
$count = 1;
until ($count > 10) {
print "Countup is: $countn";
$count++;
}
@colors = ('red', 'blue','yellow');
foreach $color (@colors) {
print "Color: $colorn";
}
Standard I/O, file I/O
Perl allows you to write to standard output, called STDOUT and read standard
input (STDIN).
=> The data coming from standard input are available in the Perl
environment via the <STDIN> descriptor
ex: $var = <STDIN>, @var = <STDIN>
=> To write to standard output just use the print() function
ex: print(“Hello world…!!!!”);
Standard I/O, file I/O
All file handles are capable of read/write access, so you can read from and
update any file or device associated with a file handle
#!/usr/bin/perl
open(DATA, "<file.txt") or die "Couldn't open file file.txt, $!";
while(<DATA>) {
print "$_";
}
read(IN, $input, 20}); # Reads 20 characters
$line = <IN>; # Reads one line
@lines = <IN>; # Reads whole file into array
Standard I/O, file I/O, stream I/O
sysopen function is similar to the main open function, except that it uses the
system open() function
sysopen(DATA, "file.txt", O_RDWR);
To close a file handle use the close function. This flushes the file handle's
buffers and closes the system's file descriptor.
close(DATA) || die "Couldn't close file properly";
O_RDONLY for opening the file
O_WRONLY for opening the file in write-only mode
O_RDWR for opening the file in read-write mode
Standard function and User-Defined function
• functions that can be called from anywhere, functions are defined with
the sub keyword
sub functionName
{
a sequence of statements
}
• In Perl, a subroutine is a separate body of code designed to perform a
particular task. A Perl program executes this body of code by calling or
invoking the subroutine
#!/usr/local/bin/perl
$total = 0;
&getnumbers;
foreach $number (@numbers)
{
$total += $number;
}
print ("the total is $totaln");
sub getnumbers {
$line = <STDIN>;
@numbers = split(/s+/, $line);
}
Standard function and User-Defined function
Standard function and User-Defined function
Perl also has many pre-defined functions
=> exists EXPR
expression that specifies an element of a hash, returns true if the
specified element in the hash has ever been initialized
ex: print "Existsn" if exists $hash{$key};
print "Definedn" if defined $hash{$key};
print "Truen" if $hash{$key};
Functions for real @ARRAYs
each, keys, pop, push, shift, splice, unshift, values
Encapsulation/Inheritance
• Public: anyone call this method
• Private: only the class can call this method
• Protected: only the class and subclasses can call this method.
Inheritance is using the properties of a parent class by a child class.
It means that a child class inherits the parent class
Encapsulation/Inheritance
use strict;
use warnings;
package Student;
sub new{
my $class = shift;
my $self = {
'name' => shift,
'roll_number' => shift
};
bless $self, $class;
return $self;
}
package Engineering;
use strict;
use warnings;
use parent 'Student';
use strict;
use warnings;
use Engineering;
my $a = Engineering ->new(“bunny",01);
print "$a->{'name'}n";
print "$a->{'roll_number'}n";
Encapsulation/Inheritance
bunny
1
Demo code
Project
http://wyvern.cs.newpaltz.edu/~konapalb1/project-PL/

Perl bhargav

  • 1.
    Programming languages Spring 2019 Introductionto PERL language Konapalli Bhargav Reddy CID - 12
  • 2.
    Topics • Brief History •Versions of Perl • Program Structure • Data Types in Perl • Standard function and User-Defined function • standard I/O, file I/O, stream I/O • Encapsulation/Inheritance • Demo
  • 3.
    Brief History • Perlstands for Practical Extraction and Report Language • Perl is the natural outgrowth of a project started by Larry Wall in 1986 • Perl languages borrow features from other programming languages including C, shell script (sh). • Perl belongs to Web Software programming domains (Scripting). • Perl is also a high-level, general-purpose, interpreted, dynamic programming languages .
  • 4.
    Versions of Perllanguage • 1987-Dec Perl 1.0 • 1988-Jun Perl 2.0 • 1989-Oct Perl 3.0 • 1991-Mar Perl 4.0 • 1994-Oct Perl 5.0 • 2014-may Perl 5.20
  • 5.
  • 6.
    Application of Perl •IMDB • Amazon.com • BBC • Booking.com • Duckduckgo
  • 7.
  • 8.
    Program Structure • Commentlines begin with: # • File Naming Scheme => filename.pl (programs) => filename.pm (modules) • Statements must end with semicolon => print("Hello world"); • Should call exit() function when finished => Exit value of zero means success exit (0); # successful => Exit value non-zero means failure exit (2); # failure
  • 9.
    Program Structure • Executinga Perl script => perl <file_name>.pl • We generally start a Perl script with the "shebang“ line #!/usr/bin/perl #!/usr/local/bin/perl #!/usr/local/bin/perl5 #!/usr/bin/perl –w The –w token is not necessary on many systems, but when it is, it is, so it's a good habit to get into.
  • 10.
    Data Types inPerl • Most data is basically either a string or a number, or a bunch of strings or numbers. • A number is just an Integer value. 1 + 18 $n = 1234; # decimal integer $n = 0b1110011; # binary integer $n = 01234; # octal integer $n = 0x1234; # hexadecimal integer $n = 12.34e-56; # exponential notation $n = "-12.34e56"; # number specified as a string $n = "1234"; # number specified as a string
  • 11.
    Data Types inPerl • A string is character data: “bhargav", "11-30-04". => Always write strings within quote marks, as above. • The concept of the variable is fundamental in all programming languages. A variable is essentially a placeholder for a value => x + 1 = 5, x is a variable Single quotes Double quotes my $name = ‘bunny'; my $time = "today"; print "Hello $name,nhow are you $time?n"; The output will be Hello bunny, how are you today? my $name = ‘bunny'; my $time = "today"; print ‘Hello $name,nhow are you $time?n’; The output will be Hello $name,nhow are you $time?n
  • 12.
    Data Types inPerl • Three most important types of variables are the scalar, the array and the hash => A scalar is a variable that holds a single value. Scalar names begin with $ ex: $name = “bhargav"; => An array is a variable that holds multiple values in series. Array names begins with @ ex: @names = (“bhargav",“bunny"); => A hash is a variable that holds pairs of data. Hash names begin with % ex: %traits = ("firstname" => "bhargav", "lastname" => "reddy");
  • 13.
    Sequence control inPerl While for $count = 10; while ($count > 0) { print "Countdown is: $countn"; $count--; } for ($count = 1 ; $count > 10 ; $count++) { print "My count is: $countn": }
  • 14.
    Sequence control inPerl until foreach $count = 1; until ($count > 10) { print "Countup is: $countn"; $count++; } @colors = ('red', 'blue','yellow'); foreach $color (@colors) { print "Color: $colorn"; }
  • 15.
    Standard I/O, fileI/O Perl allows you to write to standard output, called STDOUT and read standard input (STDIN). => The data coming from standard input are available in the Perl environment via the <STDIN> descriptor ex: $var = <STDIN>, @var = <STDIN> => To write to standard output just use the print() function ex: print(“Hello world…!!!!”);
  • 16.
    Standard I/O, fileI/O All file handles are capable of read/write access, so you can read from and update any file or device associated with a file handle #!/usr/bin/perl open(DATA, "<file.txt") or die "Couldn't open file file.txt, $!"; while(<DATA>) { print "$_"; } read(IN, $input, 20}); # Reads 20 characters $line = <IN>; # Reads one line @lines = <IN>; # Reads whole file into array
  • 17.
    Standard I/O, fileI/O, stream I/O sysopen function is similar to the main open function, except that it uses the system open() function sysopen(DATA, "file.txt", O_RDWR); To close a file handle use the close function. This flushes the file handle's buffers and closes the system's file descriptor. close(DATA) || die "Couldn't close file properly"; O_RDONLY for opening the file O_WRONLY for opening the file in write-only mode O_RDWR for opening the file in read-write mode
  • 18.
    Standard function andUser-Defined function • functions that can be called from anywhere, functions are defined with the sub keyword sub functionName { a sequence of statements } • In Perl, a subroutine is a separate body of code designed to perform a particular task. A Perl program executes this body of code by calling or invoking the subroutine
  • 19.
    #!/usr/local/bin/perl $total = 0; &getnumbers; foreach$number (@numbers) { $total += $number; } print ("the total is $totaln"); sub getnumbers { $line = <STDIN>; @numbers = split(/s+/, $line); } Standard function and User-Defined function
  • 20.
    Standard function andUser-Defined function Perl also has many pre-defined functions => exists EXPR expression that specifies an element of a hash, returns true if the specified element in the hash has ever been initialized ex: print "Existsn" if exists $hash{$key}; print "Definedn" if defined $hash{$key}; print "Truen" if $hash{$key}; Functions for real @ARRAYs each, keys, pop, push, shift, splice, unshift, values
  • 21.
    Encapsulation/Inheritance • Public: anyonecall this method • Private: only the class can call this method • Protected: only the class and subclasses can call this method. Inheritance is using the properties of a parent class by a child class. It means that a child class inherits the parent class
  • 22.
    Encapsulation/Inheritance use strict; use warnings; packageStudent; sub new{ my $class = shift; my $self = { 'name' => shift, 'roll_number' => shift }; bless $self, $class; return $self; } package Engineering; use strict; use warnings; use parent 'Student'; use strict; use warnings; use Engineering; my $a = Engineering ->new(“bunny",01); print "$a->{'name'}n"; print "$a->{'roll_number'}n";
  • 23.
  • 24.
  • 25.