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 belowdie $! unless open(FILE);#same as abovedie $! unless open(FILE);#same as abovedie $! 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 modulesIt 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/ orhttp://www.activestate.com/activeperl/downloads
● Click twice on strawberry-perl-5.XX.X.X-32bit.msiorActivePerl-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 programprint "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 commentprint "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 middleprint "Helloprint "Helloworld";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, $name\n"; # works fineprint "Hello, $name\n"; # works fineprint 'Hello, $name\n'; # prints $name\n literallyprint 'Hello, $name\n'; # prints $name\n 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, world\n");print("Hello, world\n");print "Hello, world\n";print "Hello, world\n";
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/perluse 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 chromatichttp://www.onyxneon.com/books/modern_perl/
● See also: books.perl.org
Introduction to Perl
Questions?