scripting languages that begin with “p”: perl, php and python les lacroix, ‘79 associate...

25
Scripting Languages Scripting Languages that Begin with “P”: that Begin with “P”: Perl, PHP and Python Perl, PHP and Python Les LaCroix, ‘79 Les LaCroix, ‘79 Associate Director of Associate Director of Network Services Network Services Carleton College Carleton College

Upload: margery-burke

Post on 29-Dec-2015

222 views

Category:

Documents


4 download

TRANSCRIPT

Scripting Languages that Scripting Languages that Begin with “P”: Begin with “P”: Perl, PHP and PythonPerl, PHP and Python

Les LaCroix, ‘79Les LaCroix, ‘79Associate Director of Associate Director of Network ServicesNetwork ServicesCarleton CollegeCarleton College

This program is brought to you by This program is brought to you by the letterthe letter

PP

and the numbersand the numbers

5, 4 and 2 5, 4 and 2

Who is this guy?Who is this guy?• <15 years as Network/Systems Administrator: <15 years as Network/Systems Administrator:

small programs (<10,000 lines of code)small programs (<10,000 lines of code)

• <10 years Software Engineer: moderate <10 years Software Engineer: moderate (100,000-500,000 lines of code) to large (100,000-500,000 lines of code) to large systems (>1,000,000 lines)systems (>1,000,000 lines)

• Programmed in C, C++, Fortran, Basic, Programmed in C, C++, Fortran, Basic, Pascal, assembly languages, Java, Perl, PHP, Pascal, assembly languages, Java, Perl, PHP, PythonPython

Who is this guy?Who is this guy?

• Worked in an AI groupWorked in an AI group

• Won a technology innovation award Won a technology innovation award while employed for a major while employed for a major computer manufacturer for computer manufacturer for designing a hardware configurator designing a hardware configurator calculuscalculus

What are scripting What are scripting languages?languages?

• Early scripting languages ran commands Early scripting languages ran commands in “batches” (e.g. DOS .BAT files, Unix in “batches” (e.g. DOS .BAT files, Unix shells: shells: shsh, , cshcsh))

• More recent languages are just high-level, More recent languages are just high-level, practical programming languagespractical programming languages

• Scripting languages are the “bread and Scripting languages are the “bread and butter” tools of network, system and web butter” tools of network, system and web administratorsadministrators

Where did these particular Where did these particular languages come from?languages come from?

• Larry needed to create reports from Larry needed to create reports from a bug tracking systema bug tracking system

• Rasmus wanted to track users Rasmus wanted to track users visiting is online resumevisiting is online resume

• Guido wanted hobby project to keep Guido wanted hobby project to keep him busy during the week around him busy during the week around ChristmasChristmas

Common featuresCommon features

• TypelessTypeless

• Similar available basic data types:Similar available basic data types:– numbersnumbers

– stringsstrings

– arraysarrays

– associative arrays (a.k.a. hashes or associative arrays (a.k.a. hashes or dictionaries)dictionaries)

Common featuresCommon features

• Dynamically sized structures (strings, Dynamically sized structures (strings, arrays, hashes)arrays, hashes)

• C-like “structs” emulated with hashesC-like “structs” emulated with hashes

• Classes, objects, methodsClasses, objects, methods

• Can be thought of as interpreted languages Can be thought of as interpreted languages (as opposed to compiled)(as opposed to compiled)

• PortabilityPortability

What are they good for?What are they good for?

• Prototyping/Rapid developmentPrototyping/Rapid development

• Small programs (<100,000 lines)Small programs (<100,000 lines)

• Web developmentWeb development

What are they not so good What are they not so good for?for?

• Writing efficient codeWriting efficient code

• Software engineering; moderate Software engineering; moderate (100,000-500,000 lines) to large programs(100,000-500,000 lines) to large programs

– no argument checking or type checkingno argument checking or type checking

– encapsulation is a convention, not enforcedencapsulation is a convention, not enforced

• Distributing applications where you want Distributing applications where you want to protect the source codeto protect the source code

Common featuresCommon features

• Extensibility in CExtensibility in C

• Common extensions:Common extensions:– file-level input/outputfile-level input/output

– math functionsmath functions

– command call-out (“system”, pipes)command call-out (“system”, pipes)

– network service connectivity (e.g. databases, network service connectivity (e.g. databases, LDAP, IMAP)LDAP, IMAP)

– CGI parsing, web server integrationCGI parsing, web server integration

PerlPerl• Larry Wall created Perl because awk didn’t Larry Wall created Perl because awk didn’t

cut itcut it

• Perl is to awk as Picard is to KirkPerl is to awk as Picard is to Kirk

• Perl is great with stringsPerl is great with strings

• Strings are good: a very common Strings are good: a very common administration practice is to take the human-administration practice is to take the human-readable output of one program and munge readable output of one program and munge it into a scriptit into a script

PerlPerl

• Perl 5.4 has been around for years; Perl 5.4 has been around for years; relatively few innovations in 5.5 and relatively few innovations in 5.5 and 5.65.6

• Took off with Web/CGITook off with Web/CGI

• OOP, extensibility as an afterthought. OOP, extensibility as an afterthought. (Implementation is reasonable (Implementation is reasonable anyway.)anyway.)

PerlPerl

• ““There is more than one way to do it.”There is more than one way to do it.”– accommodates multiple programming accommodates multiple programming

paradigmsparadigms

– can lead to obfuscated coding (see later can lead to obfuscated coding (see later examples)examples)

• Perl Poetry: poems have been written in Perl Poetry: poems have been written in Perl and have appeared in non-computer Perl and have appeared in non-computer publications (e.g., publications (e.g., The EconomistThe Economist))

Why is Perl good with Why is Perl good with strings?strings?

• Simple, built-in operations for string Simple, built-in operations for string transliteration, replication, and regular-transliteration, replication, and regular-expression pattern matching (with or expression pattern matching (with or without substitution)without substitution)

– transliteration: $variable =~ tr/A-Z/a-z/transliteration: $variable =~ tr/A-Z/a-z/

– replication: $variable x 5replication: $variable x 5

– r.e. matching: $variable =~ /pattern/r.e. matching: $variable =~ /pattern/

– with subs.: $var =~ s/pattern/replacement/with subs.: $var =~ s/pattern/replacement/

ExamplesExamples

– reverse the letters of each word in a reverse the letters of each word in a string, leaving everything else intact:string, leaving everything else intact:• $v =~ s/\w+/reverse($&)/eg$v =~ s/\w+/reverse($&)/eg

– exchange first two words in the string, exchange first two words in the string, leaving spacing and punctuation intact:leaving spacing and punctuation intact:• $v =~ s/(\w+)(\W+)(\w+)/$3$2$1/$v =~ s/(\w+)(\W+)(\w+)/$3$2$1/

So what?So what?• Simple operations are trivialSimple operations are trivial

• Complex patterns can be expressed Complex patterns can be expressed as terms with spacing and coding to as terms with spacing and coding to make things legible. (Other make things legible. (Other languages insist that patterns are languages insist that patterns are fundamentally strings.)fundamentally strings.)

So what?So what?

• Matching is always a multi-valued Matching is always a multi-valued operation (did anything match, what operation (did anything match, what matched, what came before and after, and matched, what came before and after, and were there submatches)were there submatches)

• Results imported transparently into local Results imported transparently into local name space or expression stack. Other name space or expression stack. Other languages have baggage to get to any of languages have baggage to get to any of it just in case you wanted to get to all of it.it just in case you wanted to get to all of it.

PHPPHP

• Started as a macro language for HTMLStarted as a macro language for HTML

• Feels like a “toy” languageFeels like a “toy” language

• Clumsy string manipulation, OOP, Clumsy string manipulation, OOP, extensibilityextensibility

• Wonderfully integrated with HTML: Wonderfully integrated with HTML: seamless intermixing of PHP and HTMLseamless intermixing of PHP and HTML

Example PHP programExample PHP program

<HTML><HTML><HEAD><TITLE>Sample PHP <HEAD><TITLE>Sample PHP Program</TITLE></HEAD>Program</TITLE></HEAD><BODY><BODY><P>The day of the week is:<P>The day of the week is:<?php<?php $current_date = getdate(time()); $current_date = getdate(time()); print $current_date[“weekday”] print $current_date[“weekday”]?></P>?></P></BODY></BODY></HTML></HTML>

PythonPython

• Designed by a credible programming Designed by a credible programming language designerlanguage designer

• Objects, classes, exception handling and Objects, classes, exception handling and extensibility are core features, not add-extensibility are core features, not add-onsons

• All things are First Class Objects: “None”, All things are First Class Objects: “None”, numbers, strings, compound data types, numbers, strings, compound data types, functions, classes, bound methodsfunctions, classes, bound methods

PythonPython

• Used for RAD (Rapid Application Used for RAD (Rapid Application Development), GUI programming, Development), GUI programming, Web programming, system Web programming, system administrationadministration

• Not great at string handlingNot great at string handling

• Very good with class inheritance, Very good with class inheritance, object persistenceobject persistence

PythonPython

• ““There are fewer ways to do it”There are fewer ways to do it”– language specifically reduces the number of language specifically reduces the number of

different, natural ways to code an different, natural ways to code an expressionexpression

– Program block structuring enforced by Program block structuring enforced by indentation rather than punctuation”{}” or indentation rather than punctuation”{}” or keywordskeywords

– Supposedly leads to more readable Supposedly leads to more readable programsprograms

Sample Python programSample Python program

Mail all Python source files (*.py) in the current directory Mail all Python source files (*.py) in the current directory to me:to me:

#!/usr/local/bin/python#!/usr/local/bin/pythonimport glob, osimport glob, osfor filename in glob.glob(‘*.py’):for filename in glob.glob(‘*.py’): print filename print filename os.system(“mail [email protected] -s %s <%s” % os.system(“mail [email protected] -s %s <%s” % (filename,filename))(filename,filename))

ConclusionConclusion

• Use the best tool: don’t drive a Use the best tool: don’t drive a screw with a hammerscrew with a hammer

• Avoid religious wars: no one is ever Avoid religious wars: no one is ever completely wrong or rightcompletely wrong or right

• Have funHave fun

• Write home oftenWrite home often