SlideShare a Scribd company logo
1 of 36
Download to read offline
Perl Programming
Course
Introduction to PerlIntroduction to Perl
Krasimir Berov
I-can.eu With the kind contribution of Chain Solutions
Contents
1. Brief History
2. Basic concepts. Interpreted (scripting) or compiled?
3. Virtual machine and platform abstraction
4. Why Perl?
5. CPAN and PPM
6. Installing on (Windows/Unix)
7. Basic syntax
8. Builtin operators and functions
9. Hello World
10.Resources
Brief History
● 1986/7 – Perl was invented by Larry Wall at NASA's Jet
Propulsion Labs
● 1987-Dec-18 Perl 1 introduced Perl to the world.
● 1988-Jun-05 Perl 2 introduced Henry Spencer's regular
expression package.
● 1989-Oct-18 Perl 3 introduced the ability to handle binary
data.
● 1991-Mar-21 Perl 4 introduced the first Camel book.
● 1994-Oct-17 Perl 5 introduced everything else,
(OOP, threads...) including the ability to introduce everything
else.
● 2014-05-27 Perl 5.20 has been released by Ricardo Signes.
Basic concepts
● P.E.R.L (Pathologically Eclectick Rubish Lister)
or
P.E.R.L (Practical Extraction and Report
Language)
● Programming Languages
● Interpreted and compiled languages
Basic concepts
● Programming languages
● C/C++
● Java
● Tcl
● Perl
● PHP
● Ruby
● JavaScript
● ….
Basic concepts
● Interpreted or Compiled is Perl?
Basic concepts
● Interpreted?
– An interpreted language needs a program called an
interpreter to process the source code every time you
run the program.
– The interpreter translates the source code down to
machine code, because it's for machines to read.
– Source code is for humans.
● Details:
– perlhack/Elements of the interpreter
– perlguts/Compiled code
● Interpreted languages: Perl, PHP, Python, Ruby...
Basic concepts
● Compiled?
– A compiled language uses a compiler to do all this
processing one time only.
– After that, you can run the produced machine code
many times on many machines without needing the
compiler.
● Compiled languages: C,C++, D, Delphy,..
● Byte-compiled languages: Java, Python, Perl (Parrot-
Perl6,Java-Perl6) :)...
● The byte code should be machine independent too
– Not as portable as Perl source
(see perlcompile, B::Bytecode).
Virtual machine
● Virtual machine == perl the program/interpreter
● The work of the interpreter has two main stages:
– compiling the code into the internal
representation (bytecode)
– executing it.
● Virtual machine for Perl 6 – Parrot is more like
Java and .NET.
● Perl6 is being ported to the Java platform too
(https://github.com/jnthn/nqp-jvm-prep).
Virtual machine
● Short breakdown of perl's work
– Compilation
● Startup
● Parsing
● Compilation and Optimization
– Run
● Running
● Exception handing
Platform abstraction
● Perl's virtual machine permits us not to think about
the specifics of the OS.
● High level of abstraction
● The same source code is run on different
platforms:
use File::Path;use File::Path;
my $dest ='/some/path/in/main/drive'my $dest ='/some/path/in/main/drive'
eval { mkpath($dest) };eval { mkpath($dest) };
if ($@) {if ($@) {
print "Couldn't create $dest:$/$@$/"print "Couldn't create $dest:$/$@$/"
. "... exiting.$/";. "... exiting.$/";
exit;exit;
}}
Why Perl?
● Easy to learn
– Learning a little Perl can get you farther than
expected.
– Easy for humans to write, rather than easy for
computers to understand.
– The syntax of the language is a lot more like a
human language .
open(FILE) or die $!; #same as belowopen(FILE) or die $!; #same as below
die $! unless open(FILE);#same as abovedie $! unless open(FILE);#same as above
die $! if not open(FILE);#same as abovedie $! if not open(FILE);#same as above
Why Perl?
● Portable
– Perl is ported to almost all modern operating systems
such as Windows, Mac OS X, Linux, Unix (created
on) and many others...
● Very high level language
– Does not make you think about obscure things like
memory allocation, CPU, etc.
Why Perl?
● „Talks“ text (in any encoding).
● „Thinks“ about files in terms of lines and sentences (by
default) or as you tell it to.
● Has powerful regular expressions built in.
if( $lines[$_] =~ /^--s*?[(w+)]/ ){if( $lines[$_] =~ /^--s*?[(w+)]/ ){
$key = $1;$key = $1;
}}
WARNING!!!
Do not write sloppy code just because it is easy to do so. In most cases
Your code lives longer than you expected and gets uglier!!!
Why Perl?
● Finally,
– Because you want so
– because your boss wants so :)...
CPAN and PPM
● Comprehensive Perl Archive Network is the
biggest source for reusable, standardized perl-
code.
Use the cpan program to install compile and
upgrade modules if you have a C compiler.
● Perl Package Manager is the ActiveState tool for
precompiled perl modules
It simplifies the task of locating, installing,
upgrading and removing Perl packages on
Windows. Use the ppm program that comes with
ActivePerl.
Installing on (Windows/Unix)
– Linux/Unix
● No need – you already have it.
● Use perlbrew to install your own Perl.
● Use your own ActivePerl.
– Windows
● Download perl for your architecture from
http://strawberryperl.com/
or
http://www.activestate.com/activeperl/downloads
● Click twice on
strawberry-perl-5.XX.X.X-32bit.msi
or
ActivePerl-5.XX.X.XXXX-....msi
1.Next, next, mm.. next, yes, next.... :D
Basic syntax
● A Perl script or program consists of one or more
statements.
● These statements are simply written in the script
in a straightforward fashion.
● There is no need to have a main() function or
anything of that kind.
Basic syntax
● Perl statements end in a semi-colon
#this is a fully functional program#this is a fully functional program
print "Hello, world";print "Hello, world";
Basic syntax
● Comments start with a hash symbol and run to
the end of the line
#this is a fully functional program with comment#this is a fully functional program with comment
print "Hello, world";print "Hello, world";
Basic syntax
● Whitespace is irrelevant
printprint
"Hello, world""Hello, world"
;;
Basic syntax
● ... except inside quoted strings
# this would print with a line-break in the middle# this would print with a line-break in the middle
print "Helloprint "Hello
world";world";
Basic syntax
● Double quotes or single quotes may be used
around literal strings
print "Hello, world";print "Hello, world";
print 'Hello, world';print 'Hello, world';
Basic syntax
● However, only double quotes "interpolate"
variables and special characters such as
newlines (n)
print "Hello, $namen"; # works fineprint "Hello, $namen"; # works fine
print 'Hello, $namen'; # prints $namen literallyprint 'Hello, $namen'; # prints $namen literally
Basic syntax
● Numbers don't need quotes around them
print 42;print 42;
Basic syntax
● You can use parentheses for functions'
arguments or omit them according to your
personal taste.
● Only required occasionally to clarify issues of
precedence.
print("Hello, worldn");print("Hello, worldn");
print "Hello, worldn";print "Hello, worldn";
Builtin operators and functions
● Perl comes with a wide selection of builtin
functions.
● Full list at the start of the perlfunc manpage.
● You can read about any given function by using
perldoc -f functionname at the
commandline.
● Perl operators are documented in full in the
perlop manpage
● Here are a few of the most common ones.
Builtin operators and functions
● Arithmetic
+ addition
- subtraction
* multiplication
/ division
Builtin operators and functions
● Numeric comparison
== equality
!= inequality
< less than
> greater than
<= less than or equal
>= greater than or equal
Builtin operators and functions
● String comparison
eq equality
ne inequality
lt less than
gt greater than
le less than or equal
ge greater than or equal
● Why separate numeric and string comparisons?
– Perl does not have special variable types.
– perl needs to know whether to sort numerically or
alphabetically.
Builtin operators and functions
● Boolean logic
&& and
|| or
! not
● and, or and not aren't just descriptions of the operators
-- they're:
– operators in their own right.
– more readable than the C-style operators
– lower precedence to && and friends.
● See perlop.
Builtin operators and functions
● Miscellaneous
= assignment
. string concatenation
x string multiplication
.. range operator (creates a list of numbers)
Builtin operators and functions
● Many operators can be combined with a = as
follows:
$a += 1; # same as $a = $a + 1$a += 1; # same as $a = $a + 1
$a -= 1; # same as $a = $a - 1$a -= 1; # same as $a = $a - 1
$a .= "n"; # same as $a = $a . "n";$a .= "n"; # same as $a = $a . "n";
Hello World
#!/usr/bin/perl#!/usr/bin/perl
use warnings;use warnings;
use strict;use strict;
use utf8;use utf8;
print 'Hi'.$/;print 'Hi'.$/;
So called shebang line.
Optional on Windows
Perl pragma to control optional warnings
Perl pragma to restrict unsafe constructs
Perl pragma to enable/disable UTF-8 in
source code (You love Unicode, right?).
Prints a string or a list of strings. A literal string(scalar value).
The input record separator, newline by
default. This influences Perl's idea of what a
"line" is.
Resources
● Perl CORE documentation
– perlhist, perlintro, perldata, perlhack, perlguts,
perlvar, perlcompile, etc.
● „Beginning Perl“ by Simon Cosens with Peter
Wainwright (Wrox Press Ltd. 2000)
http://www.perl.org/books/beginning-perl/
● Modern Perl by chromatic
http://www.onyxneon.com/books/modern_perl/
● See also: books.perl.org
Introduction to Perl
Questions?

More Related Content

What's hot

Programming in Computational Biology
Programming in Computational BiologyProgramming in Computational Biology
Programming in Computational Biology
AtreyiB
 

What's hot (20)

Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
 
Perl Introduction
Perl IntroductionPerl Introduction
Perl Introduction
 
Perl
PerlPerl
Perl
 
Programming in Computational Biology
Programming in Computational BiologyProgramming in Computational Biology
Programming in Computational Biology
 
Streams, sockets and filters oh my!
Streams, sockets and filters oh my!Streams, sockets and filters oh my!
Streams, sockets and filters oh my!
 
Perl Programming - 02 Regular Expression
Perl Programming - 02 Regular ExpressionPerl Programming - 02 Regular Expression
Perl Programming - 02 Regular Expression
 
Spl in the wild
Spl in the wildSpl in the wild
Spl in the wild
 
PHP7 is coming
PHP7 is comingPHP7 is coming
PHP7 is coming
 
SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09
 
Scalar data types
Scalar data typesScalar data types
Scalar data types
 
Pearl
PearlPearl
Pearl
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Introduction to php php++
Introduction to php php++Introduction to php php++
Introduction to php php++
 
Hacking with hhvm
Hacking with hhvmHacking with hhvm
Hacking with hhvm
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
PHP - Introduction to PHP Functions
PHP -  Introduction to PHP FunctionsPHP -  Introduction to PHP Functions
PHP - Introduction to PHP Functions
 
Php extensions
Php extensionsPhp extensions
Php extensions
 
Power of Puppet 4
Power of Puppet 4Power of Puppet 4
Power of Puppet 4
 
Subroutines
SubroutinesSubroutines
Subroutines
 
Writing and using php streams and sockets tek11
Writing and using php streams and sockets   tek11Writing and using php streams and sockets   tek11
Writing and using php streams and sockets tek11
 

Viewers also liked

FINAL-PEBMETAL-BROCHER
FINAL-PEBMETAL-BROCHERFINAL-PEBMETAL-BROCHER
FINAL-PEBMETAL-BROCHER
santosh kumar
 
Las piedras preciosas
Las piedras preciosas Las piedras preciosas
Las piedras preciosas
くん くん
 
Baila como-si-nadie-te-viera
Baila como-si-nadie-te-vieraBaila como-si-nadie-te-viera
Baila como-si-nadie-te-viera
Sherlene Reiv
 
Partes internas del cpu victoria
Partes internas del cpu victoriaPartes internas del cpu victoria
Partes internas del cpu victoria
Amabilia
 
Análisis e interpretación de estados financieros
Análisis e interpretación de estados financierosAnálisis e interpretación de estados financieros
Análisis e interpretación de estados financieros
Marco_Ontiveros
 

Viewers also liked (14)

Moo the universe and everything
Moo the universe and everythingMoo the universe and everything
Moo the universe and everything
 
Introduction to OO Perl with Moose
Introduction to OO Perl with MooseIntroduction to OO Perl with Moose
Introduction to OO Perl with Moose
 
FINAL-PEBMETAL-BROCHER
FINAL-PEBMETAL-BROCHERFINAL-PEBMETAL-BROCHER
FINAL-PEBMETAL-BROCHER
 
Negocio propio enzactalargo
Negocio propio enzactalargoNegocio propio enzactalargo
Negocio propio enzactalargo
 
Jornadas Monitor Turespaña Mercado Britanico
Jornadas Monitor Turespaña Mercado BritanicoJornadas Monitor Turespaña Mercado Britanico
Jornadas Monitor Turespaña Mercado Britanico
 
El Pintor Musical De Venecia
El Pintor Musical De VeneciaEl Pintor Musical De Venecia
El Pintor Musical De Venecia
 
Dashboard analytics 4th quarter 2015
Dashboard analytics 4th quarter 2015Dashboard analytics 4th quarter 2015
Dashboard analytics 4th quarter 2015
 
A Pathway2Work Success Story - A Testimonial from an employer for the Work Pr...
A Pathway2Work Success Story - A Testimonial from an employer for the Work Pr...A Pathway2Work Success Story - A Testimonial from an employer for the Work Pr...
A Pathway2Work Success Story - A Testimonial from an employer for the Work Pr...
 
Las piedras preciosas
Las piedras preciosas Las piedras preciosas
Las piedras preciosas
 
Baila como-si-nadie-te-viera
Baila como-si-nadie-te-vieraBaila como-si-nadie-te-viera
Baila como-si-nadie-te-viera
 
Partes internas del cpu victoria
Partes internas del cpu victoriaPartes internas del cpu victoria
Partes internas del cpu victoria
 
Análisis e interpretación de estados financieros
Análisis e interpretación de estados financierosAnálisis e interpretación de estados financieros
Análisis e interpretación de estados financieros
 
IS740 Chapter 10
IS740 Chapter 10IS740 Chapter 10
IS740 Chapter 10
 
TRATAMIENTO PERCUTÁNEO DE LA HIDATIDOSIS HEPÁTICA
TRATAMIENTO PERCUTÁNEO DE LA HIDATIDOSIS HEPÁTICATRATAMIENTO PERCUTÁNEO DE LA HIDATIDOSIS HEPÁTICA
TRATAMIENTO PERCUTÁNEO DE LA HIDATIDOSIS HEPÁTICA
 

Similar to Introduction to Perl

Similar to Introduction to Perl (20)

Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one liners
 
Cs3430 lecture 15
Cs3430 lecture 15Cs3430 lecture 15
Cs3430 lecture 15
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introduction
 
Introduction to perl_ a scripting language
Introduction to perl_ a scripting languageIntroduction to perl_ a scripting language
Introduction to perl_ a scripting language
 
Intro to Perl
Intro to PerlIntro to Perl
Intro to Perl
 
Bioinformatics p4-io v2013-wim_vancriekinge
Bioinformatics p4-io v2013-wim_vancriekingeBioinformatics p4-io v2013-wim_vancriekinge
Bioinformatics p4-io v2013-wim_vancriekinge
 
What we can learn from Rebol?
What we can learn from Rebol?What we can learn from Rebol?
What we can learn from Rebol?
 
Perly Parsing with Regexp::Grammars
Perly Parsing with Regexp::GrammarsPerly Parsing with Regexp::Grammars
Perly Parsing with Regexp::Grammars
 
ppt7
ppt7ppt7
ppt7
 
ppt2
ppt2ppt2
ppt2
 
name name2 n
name name2 nname name2 n
name name2 n
 
name name2 n2
name name2 n2name name2 n2
name name2 n2
 
test ppt
test ppttest ppt
test ppt
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt21
ppt21ppt21
ppt21
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt17
ppt17ppt17
ppt17
 
ppt30
ppt30ppt30
ppt30
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.ppt
 
ppt18
ppt18ppt18
ppt18
 

More from Krasimir Berov (Красимир Беров)

More from Krasimir Berov (Красимир Беров) (12)

Хешове
ХешовеХешове
Хешове
 
Списъци и масиви
Списъци и масивиСписъци и масиви
Списъци и масиви
 
Скаларни типове данни
Скаларни типове данниСкаларни типове данни
Скаларни типове данни
 
Въведение в Perl
Въведение в PerlВъведение в Perl
Въведение в Perl
 
System Programming and Administration
System Programming and AdministrationSystem Programming and Administration
System Programming and Administration
 
Network programming
Network programmingNetwork programming
Network programming
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Working with databases
Working with databasesWorking with databases
Working with databases
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
 
Syntax
SyntaxSyntax
Syntax
 
Hashes
HashesHashes
Hashes
 
Lists and arrays
Lists and arraysLists and arrays
Lists and arrays
 

Recently uploaded

Recently uploaded (20)

Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

Introduction to Perl

  • 1. Perl Programming Course Introduction to PerlIntroduction to Perl Krasimir Berov I-can.eu With the kind contribution of Chain Solutions
  • 2. Contents 1. Brief History 2. Basic concepts. Interpreted (scripting) or compiled? 3. Virtual machine and platform abstraction 4. Why Perl? 5. CPAN and PPM 6. Installing on (Windows/Unix) 7. Basic syntax 8. Builtin operators and functions 9. Hello World 10.Resources
  • 3. Brief History ● 1986/7 – Perl was invented by Larry Wall at NASA's Jet Propulsion Labs ● 1987-Dec-18 Perl 1 introduced Perl to the world. ● 1988-Jun-05 Perl 2 introduced Henry Spencer's regular expression package. ● 1989-Oct-18 Perl 3 introduced the ability to handle binary data. ● 1991-Mar-21 Perl 4 introduced the first Camel book. ● 1994-Oct-17 Perl 5 introduced everything else, (OOP, threads...) including the ability to introduce everything else. ● 2014-05-27 Perl 5.20 has been released by Ricardo Signes.
  • 4. Basic concepts ● P.E.R.L (Pathologically Eclectick Rubish Lister) or P.E.R.L (Practical Extraction and Report Language) ● Programming Languages ● Interpreted and compiled languages
  • 5. Basic concepts ● Programming languages ● C/C++ ● Java ● Tcl ● Perl ● PHP ● Ruby ● JavaScript ● ….
  • 6. Basic concepts ● Interpreted or Compiled is Perl?
  • 7. Basic concepts ● Interpreted? – An interpreted language needs a program called an interpreter to process the source code every time you run the program. – The interpreter translates the source code down to machine code, because it's for machines to read. – Source code is for humans. ● Details: – perlhack/Elements of the interpreter – perlguts/Compiled code ● Interpreted languages: Perl, PHP, Python, Ruby...
  • 8. Basic concepts ● Compiled? – A compiled language uses a compiler to do all this processing one time only. – After that, you can run the produced machine code many times on many machines without needing the compiler. ● Compiled languages: C,C++, D, Delphy,.. ● Byte-compiled languages: Java, Python, Perl (Parrot- Perl6,Java-Perl6) :)... ● The byte code should be machine independent too – Not as portable as Perl source (see perlcompile, B::Bytecode).
  • 9. Virtual machine ● Virtual machine == perl the program/interpreter ● The work of the interpreter has two main stages: – compiling the code into the internal representation (bytecode) – executing it. ● Virtual machine for Perl 6 – Parrot is more like Java and .NET. ● Perl6 is being ported to the Java platform too (https://github.com/jnthn/nqp-jvm-prep).
  • 10. Virtual machine ● Short breakdown of perl's work – Compilation ● Startup ● Parsing ● Compilation and Optimization – Run ● Running ● Exception handing
  • 11. Platform abstraction ● Perl's virtual machine permits us not to think about the specifics of the OS. ● High level of abstraction ● The same source code is run on different platforms: use File::Path;use File::Path; my $dest ='/some/path/in/main/drive'my $dest ='/some/path/in/main/drive' eval { mkpath($dest) };eval { mkpath($dest) }; if ($@) {if ($@) { print "Couldn't create $dest:$/$@$/"print "Couldn't create $dest:$/$@$/" . "... exiting.$/";. "... exiting.$/"; exit;exit; }}
  • 12. Why Perl? ● Easy to learn – Learning a little Perl can get you farther than expected. – Easy for humans to write, rather than easy for computers to understand. – The syntax of the language is a lot more like a human language . open(FILE) or die $!; #same as belowopen(FILE) or die $!; #same as below die $! unless open(FILE);#same as abovedie $! unless open(FILE);#same as above die $! if not open(FILE);#same as abovedie $! if not open(FILE);#same as above
  • 13. Why Perl? ● Portable – Perl is ported to almost all modern operating systems such as Windows, Mac OS X, Linux, Unix (created on) and many others... ● Very high level language – Does not make you think about obscure things like memory allocation, CPU, etc.
  • 14. Why Perl? ● „Talks“ text (in any encoding). ● „Thinks“ about files in terms of lines and sentences (by default) or as you tell it to. ● Has powerful regular expressions built in. if( $lines[$_] =~ /^--s*?[(w+)]/ ){if( $lines[$_] =~ /^--s*?[(w+)]/ ){ $key = $1;$key = $1; }} WARNING!!! Do not write sloppy code just because it is easy to do so. In most cases Your code lives longer than you expected and gets uglier!!!
  • 15. Why Perl? ● Finally, – Because you want so – because your boss wants so :)...
  • 16. CPAN and PPM ● Comprehensive Perl Archive Network is the biggest source for reusable, standardized perl- code. Use the cpan program to install compile and upgrade modules if you have a C compiler. ● Perl Package Manager is the ActiveState tool for precompiled perl modules It simplifies the task of locating, installing, upgrading and removing Perl packages on Windows. Use the ppm program that comes with ActivePerl.
  • 17. Installing on (Windows/Unix) – Linux/Unix ● No need – you already have it. ● Use perlbrew to install your own Perl. ● Use your own ActivePerl. – Windows ● Download perl for your architecture from http://strawberryperl.com/ or http://www.activestate.com/activeperl/downloads ● Click twice on strawberry-perl-5.XX.X.X-32bit.msi or ActivePerl-5.XX.X.XXXX-....msi 1.Next, next, mm.. next, yes, next.... :D
  • 18. Basic syntax ● A Perl script or program consists of one or more statements. ● These statements are simply written in the script in a straightforward fashion. ● There is no need to have a main() function or anything of that kind.
  • 19. Basic syntax ● Perl statements end in a semi-colon #this is a fully functional program#this is a fully functional program print "Hello, world";print "Hello, world";
  • 20. Basic syntax ● Comments start with a hash symbol and run to the end of the line #this is a fully functional program with comment#this is a fully functional program with comment print "Hello, world";print "Hello, world";
  • 21. Basic syntax ● Whitespace is irrelevant printprint "Hello, world""Hello, world" ;;
  • 22. Basic syntax ● ... except inside quoted strings # this would print with a line-break in the middle# this would print with a line-break in the middle print "Helloprint "Hello world";world";
  • 23. Basic syntax ● Double quotes or single quotes may be used around literal strings print "Hello, world";print "Hello, world"; print 'Hello, world';print 'Hello, world';
  • 24. Basic syntax ● However, only double quotes "interpolate" variables and special characters such as newlines (n) print "Hello, $namen"; # works fineprint "Hello, $namen"; # works fine print 'Hello, $namen'; # prints $namen literallyprint 'Hello, $namen'; # prints $namen literally
  • 25. Basic syntax ● Numbers don't need quotes around them print 42;print 42;
  • 26. Basic syntax ● You can use parentheses for functions' arguments or omit them according to your personal taste. ● Only required occasionally to clarify issues of precedence. print("Hello, worldn");print("Hello, worldn"); print "Hello, worldn";print "Hello, worldn";
  • 27. Builtin operators and functions ● Perl comes with a wide selection of builtin functions. ● Full list at the start of the perlfunc manpage. ● You can read about any given function by using perldoc -f functionname at the commandline. ● Perl operators are documented in full in the perlop manpage ● Here are a few of the most common ones.
  • 28. Builtin operators and functions ● Arithmetic + addition - subtraction * multiplication / division
  • 29. Builtin operators and functions ● Numeric comparison == equality != inequality < less than > greater than <= less than or equal >= greater than or equal
  • 30. Builtin operators and functions ● String comparison eq equality ne inequality lt less than gt greater than le less than or equal ge greater than or equal ● Why separate numeric and string comparisons? – Perl does not have special variable types. – perl needs to know whether to sort numerically or alphabetically.
  • 31. Builtin operators and functions ● Boolean logic && and || or ! not ● and, or and not aren't just descriptions of the operators -- they're: – operators in their own right. – more readable than the C-style operators – lower precedence to && and friends. ● See perlop.
  • 32. Builtin operators and functions ● Miscellaneous = assignment . string concatenation x string multiplication .. range operator (creates a list of numbers)
  • 33. Builtin operators and functions ● Many operators can be combined with a = as follows: $a += 1; # same as $a = $a + 1$a += 1; # same as $a = $a + 1 $a -= 1; # same as $a = $a - 1$a -= 1; # same as $a = $a - 1 $a .= "n"; # same as $a = $a . "n";$a .= "n"; # same as $a = $a . "n";
  • 34. Hello World #!/usr/bin/perl#!/usr/bin/perl use warnings;use warnings; use strict;use strict; use utf8;use utf8; print 'Hi'.$/;print 'Hi'.$/; So called shebang line. Optional on Windows Perl pragma to control optional warnings Perl pragma to restrict unsafe constructs Perl pragma to enable/disable UTF-8 in source code (You love Unicode, right?). Prints a string or a list of strings. A literal string(scalar value). The input record separator, newline by default. This influences Perl's idea of what a "line" is.
  • 35. Resources ● Perl CORE documentation – perlhist, perlintro, perldata, perlhack, perlguts, perlvar, perlcompile, etc. ● „Beginning Perl“ by Simon Cosens with Peter Wainwright (Wrox Press Ltd. 2000) http://www.perl.org/books/beginning-perl/ ● Modern Perl by chromatic http://www.onyxneon.com/books/modern_perl/ ● See also: books.perl.org