.
Developer Spot - Web Development Tutorials
 


Web Hosting Directory
Budget Web Hosting Linux Web Hosting Small Business Hosting
Windows Web Hosting Reseller Web Hosting Web Hosting Articles

Choosing The Right Server-Side Scripting Language

By Craig McElwee
2004-01-06
Reader Rating: 4 out of 5
Bookmark Print Version
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:


out.println(joined);


Perl:


print OUT "$joined\n";


PHP:


fwrite($out, $joined);


Python:


out.write (joined)


Tcl:


puts $out $joined




Article Pages:
How Five Languages Do The Same Basic Tasks
Task 1: Get and Format The Time/Date
Task 2: Put Form Field Data Into Variables
Task 3: Search and Replace
Task 4: File Writing
Task 5: File Reading
Task 6: Split Comma-Delimited Line Into Variables
So, Which One Should You Use?
Resources

First published by IBM developerWorks


 Rate this article:   Poor          Excellent 


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



 
Development Tutorials
ASP
CGI & Perl
CSS
HTML
Java
JavaScript
Linux
PHP
XML




More Resources
Web Hosting Articles
Development Tutorials: CGI & Perl - CSS - HTML - Java - JavaScript - Linux - PHP - XML