Choosing The Right Server-Side Scripting Language
By Craig McElwee
2004-01-06
Reader Rating:

Task 4: File Writing
This is rather straightforward and similar in all of these languages, except Java servlets. Compare how each one opens a file called "file.txt" in append mode and assigns the name "out" to the filehandle:
Java:
PrintWriter
out = new PrintWriter(new FileOutputStream("file.txt",true)); |
Perl:
open (OUT, ">>file.txt");
|
PHP:
$out = fopen("file.txt", "a");
|
Python:
out = open("file.txt", 'a')
|
Tcl:
set out [open "file.txt" a+]
|
Notice how the PHP, Python, and Tcl code snippets are simple, intuitive, and almost identical. The Perl code is extremely similar and just as easy and intuitive. Contrast this with the Java technology's "There's a class for that" approach. In fact, there are 60 classes for that. You must decide exactly which type of output stream you want, and then which particular print writer to use for every I/O situation instead of letting the language do the work. Furthermore, is it intuitive that the "true" at the end of the statement means to append? PHP, Python, and Tcl use 'a' for append, and the use of the filehandle is easily understood -- in fewer than half the keystrokes.
The task of writing to the file is rather similar across the board as demonstrated here (writing to the file handle "out" the contents of the variable "joined"):
Java:
Perl:
PHP:
Python:
Tcl:
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
|