Introduction to Perl

  • 1,516 views
Uploaded on

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

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

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,516
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
2
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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
  • 36. Introduction to Perl Questions?