1 / 25

This program is brought to you by the letter P and the numbers 5, 4 and 2

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

kathrynk
Download Presentation

This program is brought to you by the letter P and the numbers 5, 4 and 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scripting Languages that Begin with “P”: Perl, PHP and PythonLes LaCroix, ‘79Associate Director of Network ServicesCarleton College

  2. This program is brought to you by the letterPand the numbers 5, 4 and 2

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

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

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

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

  7. Common features • Typeless • Similar available basic data types: • numbers • strings • arrays • associative arrays (a.k.a. hashes or dictionaries)

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

  9. What are they good for? • Prototyping/Rapid development • Small programs (<100,000 lines) • Web development

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

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

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

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

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

  15. 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/

  16. 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/

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

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

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

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

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

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

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

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

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

More Related