Choosing The Right Server-Side Scripting Language
By Craig McElwee
How Five Languages Do The Same Basic Tasks
This article compares five prominent scripting tools (Perl, PHP, Python, Tcl, and Java servlets) by applying each to the same six common server-side tasks. You can look at the syntax side-by-side and evaluate how each language handles specific jobs. If you are new to server-side scripting, or if you have used only a few of these languages, you can see what they look like. Even if you already have a favorite, you can see how the other languages stack up in terms of usability, functionality, and code readability.
Assuming your Web site currently serves up dynamic content, how did you pick your scripting language from the numerous open source server-side scripting methodologies available? Did you inherit it or receive it from on high? Or did you methodically look at the pros and cons of each option, weigh them against your site's needs, deadlines, and your own skill set, and choose the one that fit best? If not, read on. You may find a scripting solution that is easier, more powerful, simpler to maintain, or just more fun.
Personally, I used the highly scientific notion of personal bias to select five languages for comparison: Java servlets, Perl, PHP, Python, and Tcl. These fall into two basic categories: Common Gateway Interface (CGI, or calling an external program that returns HTML) and what I call Super Markup (an HTML page embedded with other-language markup code -- a superset of HTML). My apologies if I left out your favorite sever-side scripting tool; mine is not an exhaustive compilation of possibilities (since practically any language can be used for CGI).
To show how they work side-by-side, we'll implement the same six tasks in each language:
Task 1: Get and format the time/date
Task 2: Put form field data into variables: receive 2 HTML form field variables
Task 3: Search and replace
Task 4: File writing: write the two values, along with the transaction date and time as a comma-separated string to a CSV text file (CSV = comma-separated values, such as: one, two, three)
Task 5: File reading: read and present all the records of said CSV file
Task 6: Split comma-delimited line into variables: split the last, just submitted, record back into individual fields for processing, in this case simply echoing back to the browser
Barring the use of a "real" database, these six tasks cover a lot of basic functionality, and you can derive most chores, with modest modifications, from them.
This article provides five scripts (see Resources), one per language, for the basic set of tasks above. Take a moment to them look over to get a feel for each before we dissect them. Gurus in any of these languages will note that I've dropped most language idioms, leaving the constructs they replace, thus favoring readability (for newcomers' benefit) over performance.
Let's go through the programs task by task.
First published by IBM developerWorks
If you found this article interesting, you may want to read these as well:
» Protecting your PHP and HTML Source Code
» Publishing Newsletters Using PHP & MySQL - 4
» Publishing Newsletters Using PHP & MySQL - 3
» Publishing Newsletter Using PHP & MySQL - 2
» Publishing Newsletters Using PHP & MySQL
» Unix Webserver Crontab Basics