introduction to perl

36
Perl Programming Course Introduction to Perl Introduction to Perl Krasimir Berov I-can.eu With the kind contribution of Chain Solutions

Upload: krasimir-berov

Post on 11-May-2015

1.873 views

Category:

Technology


1 download

DESCRIPTION

This is the first set of slightly updated slides from a Perl programming course that I held some years ago for the QA team of a big international company. I want to share it with everyone looking for intransitive Perl-knowledge. The updates after 1st of June 2014 are made with the kind support of Chain Solutions (http://chainsolutions.net/) A table of content for all presentations can be found at i-can.eu. The source code for the examples and the presentations in ODP format are on https://github.com/kberov/PerlProgrammingCourse

TRANSCRIPT

Page 1: Introduction to Perl

Perl Programming Course

Introduction to PerlIntroduction to Perl

Krasimir Berov

I-can.eu With the kind contribution of Chain Solutions

Page 2: Introduction to Perl

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

Page 3: Introduction to Perl

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.

Page 4: Introduction to Perl

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

Page 5: Introduction to Perl

Basic concepts

● Programming languages● C/C++● Java● Tcl● Perl● PHP● Ruby● JavaScript● ….

Page 6: Introduction to Perl

Basic concepts

● Interpreted or Compiled is Perl?

Page 7: Introduction to 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...

Page 8: Introduction to Perl

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).

Page 9: Introduction to Perl

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).

Page 10: Introduction to Perl

Virtual machine

● Short breakdown of perl's work– Compilation

● Startup● Parsing● Compilation and Optimization

– Run● Running● Exception handing

Page 11: Introduction to Perl

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;}}

Page 12: Introduction to Perl

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

Page 13: Introduction to Perl

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.

Page 14: Introduction to Perl

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!!!

Page 15: Introduction to Perl

Why Perl?

● Finally, – Because you want so

– because your boss wants so :)...

Page 16: Introduction to Perl

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.

Page 17: Introduction to Perl

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

Page 18: Introduction to Perl

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.

Page 19: Introduction to Perl

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";

Page 20: Introduction to Perl

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";

Page 21: Introduction to Perl

Basic syntax

● Whitespace is irrelevant

printprint "Hello, world""Hello, world" ;;

Page 22: Introduction to Perl

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";

Page 23: Introduction to Perl

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';

Page 24: Introduction to Perl

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

Page 25: Introduction to Perl

Basic syntax

● Numbers don't need quotes around them

print 42;print 42;

Page 26: Introduction to Perl

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";

Page 27: Introduction to Perl

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.

Page 28: Introduction to Perl

Builtin operators and functions● Arithmetic

+ addition

- subtraction

* multiplication

/ division

Page 29: Introduction to Perl

Builtin operators and functions● Numeric comparison

== equality

!= inequality

< less than

> greater than

<= less than or equal

>= greater than or equal

Page 30: Introduction to Perl

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.

Page 31: Introduction to Perl

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.

Page 32: Introduction to Perl

Builtin operators and functions● Miscellaneous

= assignment

. string concatenation

x string multiplication

.. range operator (creates a list of numbers)

Page 33: Introduction to Perl

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";

Page 34: Introduction to Perl

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.

Page 35: Introduction to Perl

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

Page 36: Introduction to Perl

Introduction to Perl

Questions?