250 likes | 260 Views
Scripting Languages that Begin with “P”: Perl, PHP and Python Les LaCroix, ‘79 Associate Director of Network Services Carleton College. This program is brought to you by the letter P and the numbers 5, 4 and 2. Who is this guy?.
E N D
Scripting Languages that Begin with “P”: Perl, PHP and PythonLes LaCroix, ‘79Associate Director of Network ServicesCarleton College
This program is brought to you by the letterPand the numbers 5, 4 and 2
Who is this guy? • <15 years as Network/Systems Administrator: small programs (<10,000 lines of code) • <10 years Software Engineer: moderate (100,000-500,000 lines of code) to large systems (>1,000,000 lines) • Programmed in C, C++, Fortran, Basic, Pascal, assembly languages, Java, Perl, PHP, Python
Who is this guy? • Worked in an AI group • Won a technology innovation award while employed for a major computer manufacturer for designing a hardware configurator calculus
What are scripting languages? • Early scripting languages ran commands in “batches” (e.g. DOS .BAT files, Unix shells: sh, csh) • More recent languages are just high-level, practical programming languages • Scripting languages are the “bread and butter” tools of network, system and web administrators
Where did these particular languages come from? • Larry needed to create reports from a bug tracking system • Rasmus wanted to track users visiting is online resume • Guido wanted hobby project to keep him busy during the week around Christmas
Common features • Typeless • Similar available basic data types: • numbers • strings • arrays • associative arrays (a.k.a. hashes or dictionaries)
Common features • Dynamically sized structures (strings, arrays, hashes) • C-like “structs” emulated with hashes • Classes, objects, methods • Can be thought of as interpreted languages (as opposed to compiled) • Portability
What are they good for? • Prototyping/Rapid development • Small programs (<100,000 lines) • Web development
What are they not so good for? • Writing efficient code • Software engineering; moderate (100,000-500,000 lines) to large programs • no argument checking or type checking • encapsulation is a convention, not enforced • Distributing applications where you want to protect the source code
Common features • Extensibility in C • Common extensions: • file-level input/output • math functions • command call-out (“system”, pipes) • network service connectivity (e.g. databases, LDAP, IMAP) • CGI parsing, web server integration
Perl • Larry Wall created Perl because awk didn’t cut it • Perl is to awk as Picard is to Kirk • Perl is great with strings • Strings are good: a very common administration practice is to take the human-readable output of one program and munge it into a script
Perl • Perl 5.4 has been around for years; relatively few innovations in 5.5 and 5.6 • Took off with Web/CGI • OOP, extensibility as an afterthought. (Implementation is reasonable anyway.)
Perl • “There is more than one way to do it.” • accommodates multiple programming paradigms • can lead to obfuscated coding (see later examples) • Perl Poetry: poems have been written in Perl and have appeared in non-computer publications (e.g., The Economist)
Why is Perl good with strings? • Simple, built-in operations for string transliteration, replication, and regular-expression pattern matching (with or without substitution) • transliteration: $variable =~ tr/A-Z/a-z/ • replication: $variable x 5 • r.e. matching: $variable =~ /pattern/ • with subs.: $var =~ s/pattern/replacement/
Examples • reverse the letters of each word in a string, leaving everything else intact: • $v =~ s/\w+/reverse($&)/eg • exchange first two words in the string, leaving spacing and punctuation intact: • $v =~ s/(\w+)(\W+)(\w+)/$3$2$1/
So what? • Simple operations are trivial • Complex patterns can be expressed as terms with spacing and coding to make things legible. (Other languages insist that patterns are fundamentally strings.)
So what? • Matching is always a multi-valued operation (did anything match, what matched, what came before and after, and were there submatches) • Results imported transparently into local name space or expression stack. Other languages have baggage to get to any of it just in case you wanted to get to all of it.
PHP • Started as a macro language for HTML • Feels like a “toy” language • Clumsy string manipulation, OOP, extensibility • Wonderfully integrated with HTML: seamless intermixing of PHP and HTML
Example PHP program <HTML><HEAD><TITLE>Sample PHP Program</TITLE></HEAD><BODY><P>The day of the week is:<?php $current_date = getdate(time()); print $current_date[“weekday”]?></P></BODY></HTML>
Python • Designed by a credible programming language designer • Objects, classes, exception handling and extensibility are core features, not add-ons • All things are First Class Objects: “None”, numbers, strings, compound data types, functions, classes, bound methods
Python • Used for RAD (Rapid Application Development), GUI programming, Web programming, system administration • Not great at string handling • Very good with class inheritance, object persistence
Python • “There are fewer ways to do it” • language specifically reduces the number of different, natural ways to code an expression • Program block structuring enforced by indentation rather than punctuation”{}” or keywords • Supposedly leads to more readable programs
Sample Python program Mail all Python source files (*.py) in the current directory to me:#!/usr/local/bin/pythonimport glob, osfor filename in glob.glob(‘*.py’): print filename os.system(“mail me@carleton.edu -s %s <%s” % (filename,filename))
Conclusion • Use the best tool: don’t drive a screw with a hammer • Avoid religious wars: no one is ever completely wrong or right • Have fun • Write home often