Scripting languages: Perl and PHP Tuukka Haapasalo December 1, 2009 - - PowerPoint PPT Presentation

scripting languages perl and php
SMART_READER_LITE
LIVE PREVIEW

Scripting languages: Perl and PHP Tuukka Haapasalo December 1, 2009 - - PowerPoint PPT Presentation

Language Popularity Perl PHP References Scripting languages: Perl and PHP Tuukka Haapasalo December 1, 2009 Tuukka Haapasalo Scripting languages: Perl and PHP Language Popularity Perl PHP References Outline 1 Language Popularity 2 Perl


slide-1
SLIDE 1

Language Popularity Perl PHP References

Scripting languages: Perl and PHP

Tuukka Haapasalo December 1, 2009

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-2
SLIDE 2

Language Popularity Perl PHP References

Outline

1 Language Popularity 2 Perl

Introduction History Syntax

3 PHP

Introduction History

4 References

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-3
SLIDE 3

Language Popularity Perl PHP References

Popularity of Programming Languages [3, 10]

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-4
SLIDE 4

Language Popularity Perl PHP References

Perl

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-5
SLIDE 5

Language Popularity Perl PHP References

Write-only code?

$_=’ev al(" seek \040D ATA ,0, 0;"); foreach (1..3) {<DATA >;}my @camel1hump ;my$camel; my$Camel ;while( <DATA >){ $_=sprintf ("% -6 9s",$_); my@dromedary 1= split (//); if(defined($ _=<DATA >)){ @camel1hum p=split (//);} while(@dromeda ry1 ){ my$camel1hump =0 ;my$CAMEL =3; if(defined($_=shif t( @dromedary1 ))&&/\S/){ $camel1hump +=1<< $CAMEL ;} $CAMEL --;if(d efined($_=shift( @dromedary1 ))&&/\S/){ $camel1hump +=1 <<$CAMEL ;}$CAMEL --;if(defined($_=shift( @camel1hump ))&&/\S/){ $camel1hump +=1<< $CAMEL ;} $CAMEL --;if( defined($_=shift( @camel1hump ))&&/\S/){ $camel1hump +=1<< $CAME L;;} $camel .=( split (// ,"\040..m‘{/J\047\134}L^7FX "))[ $camel1h ump ];} $camel .="\n";} @camel1hump =split (/\n/,$camel ); foreach(@ camel1hump ){ chomp;$Camel=$_;y/LJF7 \173\175 ‘\047/\061\062\063\ 064\065\066\067\070/; y/12345678/ JL7F \175\173\047 ‘/; $_=reverse; print"$_ \040 $Camel\n";} foreach(@camel1hump ){ chomp;$Camel=$_;y /LJF7 \173\175 ‘\047/12345678/; y/12345678/ JL7F \175\173\0 47 ‘/; $_=reverse;print "\040 $_$Camel\n";}’;;s/\s*//g;; eval; eval ("seek \040 DATA ,0 ,0;"); undef$ /;$_=<DATA >;s/\s*//g;( );;s ;^.*_;;; map{eval"print \"$_\"";}/.{4}/g; __DATA__ \124 \1 50\145\040\165\163\145\040\157\1 46\040\1 41\0 40\143\141 \155\145\1 54\040\1 51\155\ 141 \147\145\0 40\151\156 \040\141 \163\16 3\ 157\143\ 151\141\16 4\151\1 57\156 \040\167 \151\164\1 50\040\ 120\1 45\162\ 154\040\15 1\163\ 040\14 1\040\1 64\162\1 41\144 \145\ 155\14 1\162\ 153\04 0\157 \146\ 040\11 7\047\ 122\1 45\15 1\154\1 54\171 \040 \046\ 012\101\16 3\16 3\15 7\143\15 1\14 1\16 4\145\163 \054 \040 \111\156\14 3\056 \040\ 125\163\145\14 4\040\ 167\1 51\164\1 50\0 40\160\ 145\162 \155\151 \163\163 \151\1 57\156\056

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-6
SLIDE 6

Language Popularity Perl PHP References

Black Perl

BEFOREHAND: close door , each window & exit; wait until time.

  • pen

spellbook , study , read (scan , select , tell us); write it , print the hex while each watches , reverse its length , write again; kill spiders , pop them , chop , split , kill them. unlink arms , shift , wait & listen (listening , wait), sort the flock (then , warn the "goats" & kill the "sheep"); kill them , dump qualms , shift moralities , values aside , each

  • ne;

die sheep! die to reverse the system you accept (reject , respect ); next step , kill the next sacrifice , each sacrifice , wait , redo ritual until "all the spirits are pleased"; do it ("as they say"). do it(* everyone *** must *** participate *** in*** forbidden **s*e*x*). return last victim; package body; exit crypt (time , times & "half a time") & close it , select (quickly) & warn your next victim; AFTERWORDS: tell nobody. wait , wait until time; wait until next year , next decade; sleep , sleep , die yourself , die at last Tuukka Haapasalo Scripting languages: Perl and PHP

slide-7
SLIDE 7

Language Popularity Perl PHP References Introduction

Introduction

Perl was originally developed by Larry Wall, a linguist working as a systems administrator for NASA [11]. Perl is based on languages including C, shell scripting (sh), AWK, and sed (and Pascal, and BASIC|PLUS, . . . ). Initial purpose was to be a replacement for an overtaxed awk. Wall used Perl as a general-purpose Unix scripting language to make report processing easier. Perl slogans [5] There’s more than one way to do it. Perl makes easy jobs easy, and the hard jobs possible.

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-8
SLIDE 8

Language Popularity Perl PHP References Introduction

Usage of Perl

Used for easy manipulation of text files, graphics programming, system administration, network programming, applications that require database access and CGI programming on the Web [11]. Nicknamed "the Swiss Army chainsaw of programming languages" due to its flexibility and adaptability. See an introduction to Perl by Stotts [9].

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-9
SLIDE 9

Language Popularity Perl PHP References Introduction

Etymology

Etymology Perl was originally named Pearl, after the Parable of the Pearl from the Gospel of Matthew. [11] Name arbitrarily chosen for its positive connotations. Renamed to Perl to differentiate from the existing programming language PEARL. Common acronyms coined afterwards (from Perl man pages):

Practical Extraction and Report Language Pathologically Eclectic Rubbish Lister

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-10
SLIDE 10

Language Popularity Perl PHP References History

History of Perl [2]

1987 Perl 1.000 released. 1988 Perl 2.000 released. 1989 Perl 3.000 released, first public Perl tutorial. 1990 Convex (computer vendor) includes Perl as part of its standard OS distribution. 1991 Perl 4.000 released, Programming Perl (1st edition) published. 1992 First release of MacPerl. 1994 Perl 5.000 released. 1995 CPAN (Comprehensive Perl Archive Network) introduced to the Perl community. 1996 Doug MacEachern introduces mod_perl, which embeds a Perl interpreter into an Apache module. 1998 The movie Sphere is released featuring a small snippet of code from the Perl FAQ.

Full release history found in Perl Programming Documentation [1].

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-11
SLIDE 11

Language Popularity Perl PHP References History

Important people

Larry Wall, author of Perl, a linguist working as systems administrator for NASA. Author also of rn (Usenet client) and

  • patch. Co-author of Programming Perl (the Camel book).

Randal Schwartz a.k.a. merlyn, author of most Perl programming

  • books. Author of the Schwartzian transform. Popularized the Just

another Perl hacker signatures. Jarkko Hietaniemi, creator and self-appointed master librarian of CPAN, author of several Perl modules, maintainer of Perl. Schwartzian transform [13]

@sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [$_, foo($_)] } @unsorted;

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-12
SLIDE 12

Language Popularity Perl PHP References History

Perl 1.000, 1987

DESCRIPTION Perl is a interpreted language optimized for scanning arbi- trary text files, extracting information from those text files, and printing reports based on that information. It’s also a good language for many system management tasks. The language is intended to be practical (easy to use, effi- cient, complete) rather than beautiful (tiny, elegant, minimal). It combines (in the author’s

  • pinion,

anyway) some

  • f the best features of C, sed, awk, and sh, so people

familiar with those languages should have little difficulty with it. (Language historians will also note some vestiges

  • f csh, Pascal, and

even BASIC|PLUS.) Expression syntax corresponds quite closely to C expression syntax. If you have a problem that would ordinarily use sed or awk

  • r

sh, but it exceeds their capabilities or must run a little fas- ter, and you don’t want to write the silly thing in C, then perl may be for you. There are also translators to turn your sed and awk scripts into perl scripts. OK, enough hype.

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-13
SLIDE 13

Language Popularity Perl PHP References History

Perl 2.000, 1988

Recursive subroutine calls. Local variables in blocks, subroutines and evals. The manual is now not lying when it says that perl is generally faster than sed. I hope. -- Elaine Ashton [2] Assignments can function as lvalues, so you can say things like ($HOST = $host) =~ tr/a-z/A-Z/; ($obj = $src) =~ s/\.c$/.o/;

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-14
SLIDE 14

Language Popularity Perl PHP References History

Perl 3.000, 1989

Correct binary data handling, functions to pack and unpack binary structures into arrays or lists. Passing values to subroutines by reference. List chopping. New functions: mkdir and rmdir, getppid, getpgrp and setpgrp, getpriority and setpriority, chroot, ioctl and fcntl, . . .

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-15
SLIDE 15

Language Popularity Perl PHP References History

Perl 4.000, 1991

No major changes in the language. Programming Perl released and became the de facto standard for the language. Perl version number changed to identify the version documented by the book.

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-16
SLIDE 16

Language Popularity Perl PHP References History

Perl 5.000, 1994

Complete rewrite of Perl. Objects. Lexical scoping available via my. eval can see the current lexical variables. Package delimiter changed from ’ to ::. New functions: abs(), chr(), uc(), ucfirst(), lc(), lcfirst(), chomp(), glob(). Pattern matching modifiers for single/multiline: m and s.

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-17
SLIDE 17

Language Popularity Perl PHP References History

CPAN [4]

Archive of over 18000 Perl software modules and documentation, most free and open-source. CPAN means either the archive itself or the automated software installer. It is said that half of Perl’s power is in the CPAN. Exhaustive standard software repository for the language, most

  • ther languages do not have as exhaustive standard libraries.

Includes also joke distributions such as the Acme::don’t module that adds a don’t function which doesn’t run code given to it.

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-18
SLIDE 18

Language Popularity Perl PHP References History

Perl 5.x and onwards [11]

Perl 5.6, 2000 64-bit support, Unicode strings, large files, our

  • keyword. Version numbering changed, 5.x is

development version if x is odd, stable if x is even. Perl 5.8, 2002 Improved Unicode, new I/O implementation, new thread implementation, better numeric accuracy. Perl 5.10, 2007 20th anniversary of Perl. New switch statement (given / where), regexp updates, smart operator ~~. Perl 6 No promise of release date. Next major release of development, Rakudo Perl, is expected in 2010. Perl 6 will differ fundamentally from Perl 5, but the intent is to “keep Perl 6 Perl”.

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-19
SLIDE 19

Language Popularity Perl PHP References Syntax

Perl Syntax

Perl examples

use strict; my $a = ’Three \\\’s: "\\\\\" ’; my $b = 20\ _000\_000; my $c = 2.5e-3; $b <=> $c; $a cmp ’Two \\\’s: "\\\\" ’; my $greet = "Hi! "; my $name = "Paul"; my $longGreet = $greet x 3; my $hi = $longGreet . $name. ", " . $greet; my $sentence = "${name}’s dog weighs between more than $c pounds."; ($v3 , $v4) = upcase($v1 , $v2 ); # this doesn ’t change $v1 and $v2 sub upcase { return unless defined wantarray ; # void context , do nothing my @parms = @_; for (@parms) { tr/a-z/A-Z/ } return wantarray ? @parms : $parms [0]; } Tuukka Haapasalo Scripting languages: Perl and PHP

slide-20
SLIDE 20

Language Popularity Perl PHP References Syntax

Arrays

Arrays may only contain scalar contents. Array examples

use strict; my @a = ("three", "word", "array"); my @b = qw/three word array /; my @c = (@a , "and", @b); $a [1] == "word"; $#b == 2; # Index

  • f

last entry $#a = 1; # Crops the length

  • f the a array

to 2 $#c =

  • 1; # Clears

the c array @a = (); # Also clears the array @a = (1 .. 100); @b = (’A’ .. ’Z’); @c = (’01’ .. ’31’); @b = @a[0 .. 4]; # 5 first elements

  • f a

@c = @a[0, 2, 10]; # Pick some elements Tuukka Haapasalo Scripting languages: Perl and PHP

slide-21
SLIDE 21

Language Popularity Perl PHP References Syntax

Hashes

Again, hashes may only contain scalar contents. Hash examples

use strict; my %table; $table{’schmoe ’} = ’joe’; $table {7.5} = 2.6; Tuukka Haapasalo Scripting languages: Perl and PHP

slide-22
SLIDE 22

Language Popularity Perl PHP References Syntax

Regular Expressions

Regexp examples

# Pattern matching if ( $currentLine =~ m/ sought_text /) { ... } # String selection $string =~ m/whatever(sought_text)whatever2 /; $soughtText = $1; # String replacement $string =~ s/ originaltext /newtext /; # Letter -by - letter translation $string =~ tr/[a,e,i,o,u,y]/[A,E,I,O,U,Y]/;

Operators in regular expressions:

?PAT? q/STR/ or qq/STR/ m/PAT/cgimosx or /PAT/cgimosx qr/STR/imosx s/PAT/REPLACE/egimosx qx/STR/ or ‘STR‘ tr/SEARCH/REPLACE/cds qw/STR/ = split(’ ’, q/STR/); y/SEARCH/REPLACE/cds

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-23
SLIDE 23

Language Popularity Perl PHP References Syntax

Other features [1]

require - require a certain version of Perl, or require that a module is imported.

  • verload - overload Perl operators.

bless - tells that the thingy (sic.) referenced by the argument is now an object. Garbage collection without cycle detection. Compilation can be affected by run-time code executed during the compile phase.

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-24
SLIDE 24

Language Popularity Perl PHP References Syntax

Objects in Perl [1]

Perl classes are simply packages. Objects of the class need to be blessed, for example by a constructor method. No specific new constructor, just use whatever method name seems appropriate. Inheritance can be implemented by the @ISA array which tells where to look if a method can’t be found from the package. Class methods are normal subroutines. Invocation via syntax $var->method().

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-25
SLIDE 25

Language Popularity Perl PHP References

PHP

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-26
SLIDE 26

Language Popularity Perl PHP References Introduction

Introduction

Main developers Rasmus Lerdorf, a Danish-Greenlandic programmer, created the first version of PHP (started 1994, released 1995). Currently works at Yahoo! Zeev Suraski and Andi Gutmans, two Israeli programmers, rewrote parser for PHP/FI 2 in 1997, released as PHP 3 in

  • 1998. Both currently work at Zend Technologies in Tel Aviv,

Israel.

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-27
SLIDE 27

Language Popularity Perl PHP References History

History

PHP [7, 8, 12]

1994 - Personal Home Page Tools (PHP Tools), personal use. [RL] 1995 - PHP / Form Interpreter (PHP/FI). First release. [RL] 1997 - PHP/FI 2. [RL] 1998 - PHP 3.0, renamed to PHP: HyperText Preprocessor. [ZS&AG] 2000 - PHP 4.0. [ZS&AG] 2004 - PHP 5.0. [ZS&AG] Future - PHP 6.0. [ZS&AG] Competitors: ASP released as an add-on to IIS via WinNT 4.0 Option Pack in 1998. JSP 1.0 specification released in 1999 as Java’s answer to PHP and ASP.

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-28
SLIDE 28

Language Popularity Perl PHP References History

Personal Home Page Tools

Created by Rasmus Lerdorf in 1994. Written in C to replace a set of Perl scripts for showing his

  • n-line résumé and for tracking access to the web site.

For personal use, not released for public.

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-29
SLIDE 29

Language Popularity Perl PHP References History

Personal Home Page / Form Interpreter

Created by Rasmus Lerdorf. First release of PHP in 1995. PHP Tools merged with Form Interpreter, called PHP/FI. Communication with databases. Could be used to create dynamic web pages.

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-30
SLIDE 30

Language Popularity Perl PHP References History

Example code

PHP/FI example [6]

<!--getenv HTTP_USER_AGENT --> <!--ifsubstr $exec_result Mozilla --> Hey , you are using Netscape!<p> <!--endif --> <!--sql database select * from table where user=’$username ’--> <!--ifless $numentries 1--> Sorry , that record does not exist <p> <!--endif exit --> You have <!--$index:0--> credits left in your account.<p> <!--include /text/footer.html --> Tuukka Haapasalo Scripting languages: Perl and PHP

slide-31
SLIDE 31

Language Popularity Perl PHP References History

PHP/FI 2.0

Created by Rasmus Lerdorf [8] and several other people. Re-write of the PHP/FI C implementation. Released in 1997. Several thousand users, installed on 50000 web sites, approx. 1 % of Internet domains.

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-32
SLIDE 32

Language Popularity Perl PHP References History

Example code

PHP/FI 2.0 example [6]

<? $name = "bob"; $db = "db"; $result = msql($db ,"select * from table where firstname=’$name ’"); $num = msql_numrows ($result ); echo "$num records found!<p>"; $i=0; while($i <$num ); echo msql_result ($result ,$i ,"fullname"); echo "<br >"; echo msql_result ($result ,$i ,"address"); echo "<br >"; $i++; endwhile; > Tuukka Haapasalo Scripting languages: Perl and PHP

slide-33
SLIDE 33

Language Popularity Perl PHP References History

PHP 3.0

A complete rewrite by Andi Gutmans and Zeev Suraski. PHP/FI 2.0 was too underpowered for the eCommerce application Gutmans and Suraski were building. Released in 1998, called PHP: HyperText Preprocessor (PHP). Strong extensibility features. Object-oriented syntax. First successful release of PHP. Tens of thousands of users, installed on hundreds of thousands of web sites, approx. 10 % of Internet domains.

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-34
SLIDE 34

Language Popularity Perl PHP References History

PHP 4.0

Again, a rewrite by Gutmans and Suraski and a development team of dozens of developers. The core is called the Zend Engine (from Zeev and Andi). Gutmans and Suraski found Zend Technologies. Support for more web servers, HTTP sessions, output buffering, secure user input handling, references, boolean type, PCRE library included. Improved speed and reliability. Used by hundreds of thousands of developers, installed on several million sites, approx. 20 % of Internet domains.

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-35
SLIDE 35

Language Popularity Perl PHP References History

PHP 5.0

Core is Zend Engine 2.0. Released in 2004. New object model, embedded SQLite, exception handling (try . . . catch), integrated SOAP support, better XML tools,

  • iterators. . .

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-36
SLIDE 36

Language Popularity Perl PHP References History

PHP 5.0 example [6]

<?php class db { protected static $dbh = false; function connect () { self :: $dbh = new PDO(’mysql:host=localhost;dbname=test ’,’user ’,’pass ’); self ::$dbh -> setAttribute (PDO:: ATTR_ERRMODE , PDO:: ERRMODE_EXCEPTION ); } } class items extends db { function load($name) { if(! self :: $dbh) $this ->connect (); try { if(! self :: $dbh) $this ->connect (); $stmt = self ::$dbh ->prepare("SELECT * FROM items WHERE firstname =: name ORDER by ctime desc"); $ret = $stmt ->execute( array(’name ’=>$name )); } catch ( PDOException $e) { die($e ->getMessage ()); } return $ret; } } $db = new items; $result = $db ->load("bob"); foreach($result ->fetch(PDO :: FETCH_ASSOC ) as $row) { echo <<<EOB {$row[’fullname ’]}<br /> EOB; } ?> Tuukka Haapasalo Scripting languages: Perl and PHP

slide-37
SLIDE 37

Language Popularity Perl PHP References History

PHP 6.0

In development since 2006. Feature changes:

Native Unicode support Magic quotes removed Global registering removed Safe mode removed Command-line scripting

Tuukka Haapasalo Scripting languages: Perl and PHP

slide-38
SLIDE 38

Language Popularity Perl PHP References

References I

[1]

  • J. Allen.

Perl programming documentation, 2009. http://perldoc.perl.org/. Referenced on 9.11.2009. [2]

  • E. Ashton.

The timeline of perl and its culture, 2001. http://history.perl.org/PerlTimeline.html. Referenced on 9.11.2009. [3] DedaSys LLC. Programming language popularity, Nov 2009. http://langpop.com/. Referenced on 18.11.2009. [4]

  • J. Hietaniemi.

Cpan - comprehensive perl archive network, 2009. http://www.cpan.org/. Referenced on 18.11.2009. [5]

  • B. M. Kuhn.

Picking Up Perl. Internet, Jan 2001. http://www.faqs.org/docs/pperl/. Referenced on 9.11.2009. [6]

  • R. Lerdorf.

Performance and security, Mar 2007. http://talks.php.net/show/oscms07/. Referenced on 19.11.2009. [7] NuSphere Corporation. The history of PHP, 2009. http://www.nusphere.com/php/php_history.htm. Referenced on 18.11.2009. Tuukka Haapasalo Scripting languages: Perl and PHP

slide-39
SLIDE 39

Language Popularity Perl PHP References

References II

[8] PHP Documentation Group. PHP manual, 2009. http://www.php.net/manual/. Referenced on 18.11.2009. [9]

  • D. Stotts.

The Perl scripting language, Dec 2002. http://rockfish-cs.cs.unc.edu/COMP144/IEPerl.pdf. Referenced on 18.11.2009. [10] Tiobe Software. TIOBE programming community index for November 2009, Nov 2009. http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html. Referenced on 18.11.2009. [11] Wikipedia. Perl, 2009. http://en.wikipedia.org/wiki/Perl. Referenced on 9.11.2009. [12] Wikipedia. PHP, 2009. http://en.wikipedia.org/wiki/PHP. Referenced on 9.11.2009. [13] Wikipedia. Schwartzian transform, 2009. http://en.wikipedia.org/wiki/Schwartzian_transform. Referenced on 18.11.2009. Tuukka Haapasalo Scripting languages: Perl and PHP