Kevin Nelson Marshall
Other entries:
« Playing with Factor ...

I'm still attempting to wrap my feebile head around all that is Factor and while I'll admit that I seem to be missing out on some obvious things (like how to access data within a Vector!); I have been able to play around enough to get some pretty neat things working.

Tonight I thought I would share with you a little comparison of a basic script done in a variety of languages to handle a task I actually do quite often in my various work - screen scraping or rather reading in an HTML file from a given URL (I'll leave the parsing bit out for now so all our script examples are going to do is grab a page and show the source).

Just about every language I know of has a way of doing this simple little task and so I thought it might be interesting to show you a generic version in three different languages (Java, Ruby, and Factor).

Assuming you've got Java, Ruby, and Factor set up on your system already here goes ... First the Java version (saved in a file called geturl.java )

import java.net.*;
import java.io.*;
public class geturl {
public static void main(String[] args) {
try {
URL url = new URL("http://www.botfu.com");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine = "";
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine); } } catch (Exception e) { } } }



Once you've got that file created and saved, drop down to your command line and compile it with the javac geturl.java command and then execute it with the java geturl command. This should dump the html source of www.botfu.com out to you in your command window.

OK so now the (much shorter) Ruby version (saved in a file called geturl.rb).

require 'net/http'
geturl = Net::HTTP.get_response(URI.parse("http://www.botfu.com"))
puts geturl.body



Once you've got that file created and saved, drop down into your command window again and run the ruby geturl.rb command and once again the HTML source of www.botfu.com should be displayed for you in your command window.

Now finally the Factor version. Start up your Factor listener (you could do this via command line as well, but since we are just doing some quick testing/demo it's much nicer in the Listener trust me). Now in the listener type in the following:

USING: http.client ; "http://www.botfu.com" http-get write



And like the previous examples you should once again get the HTML source of www.botfu.com displayed to you (this time within the listener since we ran the command from within there).

And yes kids, it's really that simple. Does one approach have an advantage over another you ask? Well that really depends on what your situation is and what you're most comfortable with ...

posted by Kevin Marshall on 2008-01-09 00:00:00+00


Subscribe to my RSS feed »

BotFu feed with RSS reader

BotFu feed by Email


Search All Posts »


Kevin Marshall - Who's That?

I'm just your basic programmer. I can't spell to save my life, I'm not the greatest story teller, and I often ramble on about nothing. This blog showcases all of that!

If you're bored drop me an email at info at falicon.com or view my outdated resume.


Stalk Kevin on »

bit.ly
Delicious
Digg
Disqus
Facebook
Flickr
FriendFeed
Github
Last.fm
LinkedIn
StumbleUpon
Twitter (@falicon)

Archives by Category »

(28) Code »
(8) ColdFusion »
(15) Database »
(10) Factor »
(3) Falcons »
(321) General »
(13) JavaScript »
(18) Perl »
(17) PHP »
(20) Ruby »

Archives by Month »

(1) September 2010 »
(2) August 2010 »
(3) July 2010 »
(13) June 2010 »
(8) May 2010 »
(2) April 2010 »
(2) March 2010 »
(5) January 2010 »
(2) October 2009 »
(6) August 2009 »
(11) July 2009 »
(2) May 2009 »
(3) April 2009 »
(2) March 2009 »
(7) February 2009 »
(9) January 2009 »
(14) December 2008 »
(5) November 2008 »
(12) October 2008 »
(13) September 2008 »
(16) August 2008 »
(23) July 2008 »
(20) June 2008 »
(24) May 2008 »
(23) April 2008 »
(27) March 2008 »
(28) February 2008 »
(26) January 2008 »
(7) December 2007 »

Published Works »

Beginning Amazon's SimpleDB (Apress in dev.)
Pro Active Record (Apress 2007)
Web Services with Rails (O'Reilly 2006).

Contributed To »

Ruby Cookbook (O'Reilly 2006)
SQL Cookbook (O'Reilly 2005)
Various Reviews published in Computing Reviews

Free Code I've Created »

SimpleDB library in Python 3.0



This blog is powered by KickAssCode.