URL Lister - My First Firefox Plugin

May 7th, 2008

URL Lister Logo

I just released my first firefox plugin - URL Lister. It shows the URLs of all the open tabs in a textarea so that they can be copied easily.

Download/Install

Install URL Lister

If you have installed it, consider rating it or reviewing it at Firefox plugins sandbox. Go to the public page for URL Lister and write a review for the application. I need reviews to promote it to the main extensions page - sandbox plugins are for registered users only.

Usage

Lets say you have these four tabs open…

Right click any tab and click on the ‘URL Lister…’ to open up the main dialog. You can also use ‘Tools > URL Lister’. You will find the URLs of all the open tabs there.

URL Lister Screenshot

There is a drop down menu at the bottom - it has these three options…

  • Plain Text
  • HTML Anchors
  • Linked List

Open URLs

If you have a list of URLs and want to open them all, all you have to do is copy those URLs into this dialog and press OK - this will open up all the given URLs.

And my thanks goes to…

URL Lister is sort of a response to this post by Matt Cutts. I want to thank him for the idea.

There is another plugin with a similar function - Tab URL Copier. I lifted some code from that plugin when creating URL Lister. Thanks.

Hello Script for Ruby

May 1st, 2008

Hello Script is a file that contains the most commonly used elements of a programming language so that it can be used as a cheatsheet when working with that language. This Hello Script for Ruby is the sixth post in this series.

I have some experience with Ruby. I will not call myself an expert - but I am comfortable with Ruby. I like ruby. I even have written a few applications in it.

Hello Code


#!/usr/bin/ruby

print "Hello World\n"

name = "Binny"
year = 2008
print "Hello, " + name + " - welcome to " + year.to_s + "\n"

if (year > 2008) then
	print "Welcome to the future - yes, we have flying cars!"
elsif (year < 2008) then
	print "The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!"
else
	print "Anything wrong with your time machine? You have not gone anywhere, kiddo."
end
print "\n\n"

# For loop like structure
0.upto(3) { |i|
	print i.to_s + ") Hi there!\n"
}
print "\n"

# Numerical array
rules = ['Do no harm', 'Obey', 'Continue Living']
i = 0
while i<rules.length do
	print “Rule ” + (i+1).to_s + “: ” + rules[i] + “\n”
	i = i+1
end
print “\n”

# Associated arrays
associated = {
	‘hello’	=>	‘world’,
	‘foo’	=>	‘bar’,
	‘lorem’	=>	‘ipsum’
}
associated.each { |key,value|
	print key + ” : ” + value + “\n”
}
print “\n”

# Using Join and Split
csv_values = “hello,world,how,are,you\n”.split(”,”)
print csv_values.join(”:”)

# Function, argument, return, call
def hello(name)
	return “Hello ” + name
end
hello_string = hello(”Binny”)
print “Function call returned ‘” + hello_string + “‘\n\n”

# One for the OOP fanboys - Class, members, object and stuff.
class Movie
	public
	@name = ”
	@rating = 0

	def initialize(name)
		@name = name
		self.rateMovie()
	end

	def rateMovie()
		@rating = (@name.length % 10) + 1 #IMDBs rating algorithm. True story!
	end

	def printMovieDetails()
		print “Movie : ” + @name + “\n”
		print “Rating : ” + ‘*’ * @rating + “(” + @rating.to_s + “)\n\n”
	end
end
# Create the object
ncfom = Movie.new(”New Country for Old Men”) #It’s a sequel!
ncfom.printMovieDetails()

# File IO
# File reading, easy method…
file_in = File.new(’Hello.rb’, ‘r’)
contents = file_in.read
file_in.close
print “Hello has ” + contents.length.to_s + ” chars\n”

# Writing to a file
file_out = File.new(’/tmp/hello.txt’, ‘w’)
file_out.print “Hello World from Ruby.”
file_out.close

# Command Executing
print `ls` #Execute the command ‘ls’ and print its output
print “\n”

# Regular Expressions
string = “Hello World”
print “Yup - its evil\n” if(/^Hell/.match(string))
print string.gsub(/l([^l])/, ‘\1′) #Remove an ‘l’ from both words. Should print ‘Helo Word’ - The second arg must be in single quotes

print “\n”

# Some special/only-in-ruby stuff…
# Using a library
require “fileutils”

#Using yield/code blocks…
def doXTimes(i)
	0.upto(i) {|count|
		yield count+1
	}
end
doXTimes(5) {|count|
	print count.to_s + “th Time\n”
}

Hello Script for Tcl

April 24th, 2008

Tcl Logo

Tcl, or Tool Command Language, will not be found in the ‘most popular languages’ hall of fame. That is partly because of its ‘wierd’ syntax. But those who know Tcl will tell you that Tcl is a pleasure to work with. I have a special interest in Tcl - its the language that introduced me to GUI programming(Tcl/Tk). Now, when ever I see a GUI toolkit, I compare it to Tcl.

I have a few years experience in Tcl - I have written a very popular Tcl/Tk Tutorial and also a few small applications in Tcl. I am not using it a lot now a days because Tk apps look really bad in Linux. This problem is not present in Windows.

Hello Code


#!/usr/bin/tclsh

# Printing(IO)
puts "Hello World!\n" 

# Variables, concatenation
set name 'Binny'
set year 2008
puts [concat "Hello, "  $name  " - welcome to "  $year]

#If,else conditions
if { $year > 2008 } {
	 puts “Welcome to the future - yes, we have flying cars!” 

} elseif { $year < 2008 }  {
	 puts “The past - please don’t change anything. Don’t step on any butterflies. And for the sake of all that’s good and holy, stay away from your parents!” 

} else {
	 puts “Anything wrong with your time machine? You have not gone anywhere, kiddo.”
}

# For loop
for { set i 0 } { $i<3 } { incr i  }  {
	 puts “$i) Hi there!”
}

#Numerical Array, foreach
set rules [list "Do no harm" "Obey" "Continue Living"]

set i 0
while { $i < [llength $rules] } {
	puts [concat "Rule " [expr $i+1] ” : ”  [lindex $rules $i]]
	incr i
}

# Associated array, while
array set associated {
	hello	“world”
	foo		“bar”
	lorem	“ipsum”
}

foreach key [array names associated] {
	 puts [concat $key " : " $associated($key)]
}

# Using Join and Split
set csv_values [split "hello,world,how,are,you\n" ","]
puts [join $csv_values ":"]

# Function, argument, return, call
proc hello { person_name } {
	return [concat "Hello, " $person_name]
}
puts [hello "Binny"]

# File IO
# File reading, easy method…
set IN [open "Hello.tcl" r]
set contents [read $IN]
close $IN
puts [concat "Hello has " [string length $contents] ” chars”]

# Writing to a file
set OUT [open "/tmp/hello.txt" w]
puts $OUT “Hello World”
close $OUT

# Regular Expressions
set str “Hello World”
if { [regexp {^Hell} $str] } {
	puts “Yup, its evil”
}

puts [regsub -all {l([^l])} $str {\1}]

# Special Tcl Syntax
# Math ops
set answer [expr {3 + 2}]

# Comments
puts $answer ;# Comments in the same line as code must use ;# instead of just #

Tcl/Tk Links

Perl Hello Script

April 10th, 2008

Perl Logo

This is the next installment of the Hello Script series - Hello Script for Perl. ‘Hello Script’ is a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet when working with that language.

Introduction to Perl

For those of who are unfamiliar with perl, here is the Wikipedia definition…

Perl is a dynamic programming language created by Larry Wall and first released in 1987. Perl borrows features from a variety of other languages including C, shell scripting (sh), AWK, sed and Lisp. Perl was widely adopted for its strengths in text processing and lack of the arbitrary limitations of many scripting languages at the time.

If you are interested in learning perl, I have written a Perl Tutorial. And here are some more links if you are interested

Hello Script for Perl


#!/usr/bin/perl
use strict;
use warnings;

# Printing(IO)
print "Hello World!\n";

# Variables, concatenation
my $name = 'Binny';
my $year = 2008;
print "Hello, " . $name . " - welcome to " . $year . "\n";

#If,else conditions
if ($year > 2008) {
	print "Welcome to the future - yes, we have flying cars!";
}
elsif($year < 2008) {
	print "The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!";
}
else {
	print "Anything wrong with your time machine? You have not gone anywhere, kiddo.";
}
print "\n";

# For loop
for(my $i=0; $i<3; $i++) {
	print "$i) Hi there!\n";
}

#Numerical Array, foreach
my @rules = (
	'Do no harm',
	'Obey',
	'Continue Living'
);
my $i = 1;
foreach my $rule (@rules) {
	print "Rule " . $i . " : " . $rule . "\n";
	$i++;
}

# Associated array, while
my %associated = (
	'hello'	=>	'world',
	'foo'	=>	'bar',
	'lorem'	=>	'ipsum'
);
while(my ($key, $value) = each(%associated)) {
	print "$key: $value\n";
}

# Using Join and Split
my @csv_values = split(',', "hello,world,how,are,you\n");
print join(":", @csv_values);

# Function, argument, return, call
sub hello {
	$name = shift; #First argument.
	return "Hello " . $name;
}
print hello("Binny");

# File IO
# File reading, easy method...
open(IN,'Hello.pl') or die("Cannot open file : $!");
my @lines = <IN>;
close(IN);
my $contents = join('',@lines);
print "Hello has " . length($contents) . " chars\n";
# Writing to a file
open(OUT, '>/tmp/hello.txt');
print OUT "Hello World";
close(OUT);

# Command Executing
print `ls`; #Execute the command 'ls' and print its output
print "\n";

# Regular Expressions
my $string = "Hello World";
print "Yup - its evil\n" if($string =~ /^Hell/);
$string =~ s/l([^l])/$1/g; #Remove an ‘l’ from both words. Should print ‘Helo Word’
print $string;

print “\n\n”;

Document Formats

April 3rd, 2008

Documents

In FLOSS circles, March 26 is celebrated as the Document Freedom Day.

Document Freedom Day (DFD) is a global day for document liberation. It will be a day of grassroots effort to educate the public about the importance of Free Document Formats and Open Standards in general.

This is when I realized that people actually use the office packages daily. I almost never use it. According to me there are three options to store text data.

  • Plain Text
  • HTML
  • Database

Plain Text

The simplest format there is. If there is something I should remember, I just put it in a text file and save it to the desktop. I used to use it a lot earlier - but I don’t use it much nowadays due to searchability issues. There are quite a few advantages in using the text format

HTML

If I need any formatting in the text, I create the document in HTML. It is easier for me to create the formatting using HTML code that using WYSIWYG Word Processors(like MS Word). I write all my blog posts in HTML - perhaps the only occasion where I need formatting.

Database

My favorite method to store text data is in a database. I am a web developer - so I always have a Web Server and Database server running on my system - so this system is perfect for me.

Interface

Remove that scared look on your face - I don’t use phpMyAdmin or any Database Administration tools as the interface to save/view the data. I use my own custom scripts or WordPress.

The best example of this is txt. Txt is my code snippets/commands repository. You can view the full story in the Saving Code Snippets post.

That’s an online example - I also have a personal wordpress blog running in my local server. I use it to record events, purchases, store receipts etc.

Advantages

Tagging
Tagging is heaven-sent to make information more findable - any del.icio.us user should know that. I used to install Ultimate Tag Warrior to get this feature - but WordPress now supports tags natively.
Searching
Searching for data within a database is much more easier, faster and provides more relevent results than searching for the data in a collection of file. Even if you are using a file indexing software like Google Desktop Search or Beagle/Recoll(for linux users), I find database searching more easier. Another advantage of using database to search is that you can create complex queries if you know SQL.

Disadvantages

Not for everyone
Let’s face it - installing and maintaining a web/database server is a tad in the geek zone. An average Joe will find it just a little bit out of their league.
Overhead
Running a web server and a database server is a bit demanding on the RAM.
Backing up a little more complicated - but easier
Backing up the data in a Database is not as straight forward as backing up files - but its actually easier if you know how.

Online

One extra method to store the data - online. This data is stored in a database - but you don’t have the disadvantages associated with using a database. More and more people are turning to this method now. I did not include it in the initial list because its not a data format - its more of a data storing method.

Python Hello Script

March 19th, 2008

Python Logo

As promised in the last post(Hello Script for PHP), this is my Hello Script for Python. Hello Script‘ is a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet when working with that language.

A word of caution here - the blocks in Python is created using whitespace - so the indentation is important. So when you see an indentation in the below code, think of it as one tab(instead of four spaces or something).

I want to insert a disclaimer here - I am not that good with python. We never really clicked. I have done very limited work in Python. So if you notice any problems with the below script, let me know and I’ll correct it.


#!/usr/bin/python

print "Hello World\n"

name = "Binny"
year = 2008
print "Hello, " + name + " - welcome to " + str(year) + "\n"

# If,else conditions
if (year > 2008):
	print "Welcome to the future - yes, we have flying cars!\n"
elif(year < 2008):
	print "The past - please don't change anything. Don't step on any butterflies. And for the sake of all thats good and holy, stay away from your parents!\n"
else:
	print "Anything wrong with your time machine? You have not gone anywhere, kiddo.\n"

# For loop
for i in range(0,3):
	print str(i) + ") Hi there!"

print ""

#Numerical Array, While
rules = ['Do no harm','Obey','Continue Living']
i = 0
while(i<len(rules)):
	print “Rule ” + str(i+1) + ” : ” + rules[i]
	i = i + 1
print “”

# Associated array, foreach
associated = {
	‘hello’	:	‘world’,
	‘foo’	:	‘bar’,
	‘lorem’	:	‘ipsum’
}
for key in associated:
	print key + ” : ” + associated[key]
print “”

import string
csv_values = string.split(”hello,world,how,are,you\n”, “,”)
print string.join(csv_values, “:”)

# Function, argument, return, call
def hello(name):
	return “Hello ” + name + “\n”

hello_string = hello(”Binny”)
print hello_string

# One for the OOP fanboys - Class, members, object and stuff.
class Movie:
	name = ”
	rating = 0

	def __init__(self, name):
		self.name = name
		self.rateMovie()

	def rateMovie(self):
		self.rating = (len(self.name) % 10) + 1 #IMDBs rating algorithm. True story!

	def printMovieDetails(self):
		print “Movie : “,  self.name
		print “Rating : “, ‘*’ * self.rating , “(”, self.rating ,”)\n”

#Create the object
ncfom = Movie(”New Country for Old Men”) #It’s a sequel!
ncfom.printMovieDetails()

# File IO
# File reading, easy method…
file_in  = open(’Hello.py’, ‘r’)
contents = file_in.read()
print “Current file has ” + str(len(contents)) + ” chars\n”
file_in.close()
# Writing to a file
file_out = open(’/tmp/hello.txt’, ‘w’)
file_out.write(”Hello World”)
file_out.close()

# Command Executing
import commands
import os
print “Result of ‘ls’ command is ” + commands.getoutput(’ls’) #Execute the command ‘ls’ and print its output
print

# Regular Expressions
import re
hell_check = re.compile(”^Hell”)
string = “Hello World”
if hell_check.match(string): print “Yup - its evil (Compiled)”
if re.match(’^Hell’, string): print “Yup - its evil (Not Compiled)”
print re.sub(r’l([^l])’, r’\1′, string)

As I said last time, save this to a file and keep it around for future reference.

Next hello script - Perl, Ruby or Tcl/Tk? Which one do you want - leave it in the comments.

Hello Script for PHP

March 15th, 2008

In the last post I introduced the concept of ‘Hello Script’ - a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet when working with that language. I thought I can elaborate on that concept by creating Hello Scripts for all the languages that I am familiar with.

Let’s start with PHP - I already provided this as an example for the last post. Here is the entire PHP Hello Script…


<?php
// Printing(IO)
print "Hello World!\n";

// Variables, concatenation
$name = 'Binny';
$year = 2008;
print "Hello, " . $name . " - welcome to " . $year . "\n";

//If,else conditions
if ($year > 2008) {
	print "Welcome to the future - yes, we have flying cars!";
}
else if($year < 2008) {
	print "The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!";
}
else {
	print "Anything wrong with your time machine? You have not gone anywhere, kiddo.";
}
print "\n";

// For loop
for($i=0; $i<3; $i++) {
	print "$i) Hi there!\n";
}

//Numerical Array, While
$rules = array(
	'Do no harm',
	'Obey',
	'Continue Living'
);
$i = 0;
while($i<count($rules)) {
	print "Rule " . ($i+1) . " : " . $rules[$i] . “\n”;
	$i++;
}

// Associated array, foreach
$associated = array(
	‘hello’	=>	‘world’,
	‘foo’	=>	‘bar’,
	‘lorem’	=>	‘ipsum’
);
foreach($associated as $key => $value) {
	print “$key: $value\n”;
}

// Using Join and Split
$csv_values = explode(’,', “hello,world,how,are,you\n”);
print implode(”:”, $csv_values);

// Function, argument, return, call
function hello($name) {
	return “Hello ” . $name;
}
$hello_string = hello(”Binny”);

// One for the OOP fanboys - Class, members, object and stuff.
class Movie {
	public $name = ”;
	public $rating = 0;

	function __construct($name) {
		$this->name = $name;
		$this->rateMovie();
	}
	function rateMovie() {
		$this->rating = (strlen($this->name) % 10) + 1; //IMDBs rating algorithm. True story!
	}

	function printMovieDetails() {
		print “Movie : {$this->name}\n”;
		print “Rating : ” . str_repeat(’*', $this->rating) . “({$this->rating})\n\n”;
	}
}
//Create the object
$ncfom = new Movie(”New Country for Old Men”); //It’s a sequel!
$ncfom->printMovieDetails();

// File IO
// File reading, easy method…
$contents = file_get_contents(’Hello.php’);
print “Hello has ” . strlen($contents) . ” chars\n”;
// Writing to a file
$file_handle = fopen(’/tmp/hello.txt’, ‘w’);
fputs($file_handle, “Hello World”);
fclose($file_handle);

// Command Executing
print `ls`; //Execute the command ‘ls’ and print its output
print “\n”;

// Regular Expressions
$string = “Hello World”;
if(preg_match(’/^Hell/’, $string)) print “Yup - its evil\n”;
print preg_replace(’/l([^l])/’, “$1″, $string); //Remove an ‘l’ from both words. Should print ‘Helo Word’

/*
 * Specialized code
 */
// Database connectivity(native)
mysql_connect(’localhost’, ‘root’, ”) or die(”Cannot connect to the Database server”);
mysql_select_db(’Data’) or die(’Could not find a database called “Data”‘);

// Executing Query
$sql_handle = mysql_query(’SELECT name,url,description FROM Comment LIMIT 1′) or die(’Query Error: ‘ . mysql_error());
while($row = mysql_fetch_assoc($sql_handle)) {
	print “Name	:	$row[name]\n”;
	print “URL	:	$row[url]\n”;
	print “Desc	:	$row[description]\n\n”;
}
/* Just a note here - if you are going to use PHP with database in a production
 * system(and you will, trust me), use a Database abstraction layer rather than
 * the above mentioned native methods.
 */

print $_REQUEST['username']; // Method to get the value of the field ‘username’ after a form submit. Will not work at CLI execution

Save this to a file and keep it around for future reference - if you are just starting out with PHP

Coming up next - Python Hello Script.

Learning a New Programming Language: The ‘Hello World’ Method

March 6th, 2008

Hello World Learning

Learning a new programming language is fun - and essential if you want to stay competitive. I try to learn new languages all the time(my latest target is Haskell). As a result, I have a system to make learning new languages easier. This is for people who already know a programming language and want to learn another one.

Requirements

What you must have…

The interpreter/compiler of the language you are learning
You must be able to run the program after writing it.
Language reference manual/documentation
Usually found at the site of the language. Put a shortcut to that on your desktop - because you will be using this all the time.
An easy to follow tutorial
Search for it in Google. If you find one and its hard to follow, ditch it and get another. You may not need this - as manual for most languages have a tutorial in them.
A ‘todo’ project that you cannot live without
This is what forces you to learn the language - more on this later.

What I would recommend you have…

Having this will help you learn - but its not necessary.

A Net Connection
You may want to ask your doubt on the IRC channel. Or on a forum. Or to google the error message you got.
A Decent IDE
This may make your job easier - but if you have notepad, that’s enough.

What you don’t need

Books on the language
You may need a book to master the language - but you don’t need one to learn it.
A Teacher
Its helpful to have someone to clarify the doubts you may have - but no one can teach you anything you cannot learn yourself.

The Hello World Script

First you have to create the ‘hello world’ application. This is more than just ‘print “Hello World”‘ - it will act as a cheatsheet for you until you familiarize yourself with the language. The point of this application is to use all the most commonly used elements of a language and putting it in a single place so that you can refer to it later.

Your first job is to go to the tutorial, the manual and the internet until you create the Hello World application with the following stuff in it. I will provide an example - how the ‘hello world’ application will look in PHP.

After each step run the application and make sure it works.

Print “Hello World\n”

Why do you think we call it a hello world application? Write the code to print a string ‘hello world’ and save it to a file. Now run it using the interpreter - and make sure it works - see it in action.


// Printing(IO)
print "Hello World!\n";
	
Comments

Put a single line comment(like //) on top of the “print ‘hello world’” code and if your language supports it, a multiline comment(/* - */) as well. Run the script to make sure the comments work as advertised.


// Single line comment.
/*
Mulitline comment.
*/
	

I was just kidding about running the script to test the comments - you did’nt do it, did you?

Use a variable(a string and an integer) and a concatenation operator.

Create a variable called name(string) - and give it your name as the value. Create another integer variable and give it the current year. You don’t have to find it programatically - just put it as 2008 or something. Now you have to print out the a string “Hello, <name> - Welcome to year <$year>”. This string will let you concatenate two different type variable to a string - and it makes me feel like a time traveler ;-).

Yeah - the value of variable name should appear at <name>. Use the concatenation operator if possible - in PHP, write

print "Hello, " . $name . " - welcome to year " . $year;

instead of

print "Hello, $name - welcome to year $year";

This is because different languages have different concatenation operators. The other stuff is the same almost universally. + refers to addition. - refers to subtraction. * is multiplication. My point is that if you know the operators for one language, you know the operators for almost all languages. But the concatenation operator differs from language to language - its ‘+’ in javascript, ruby. Its ‘.’ in Perl, PHP, etc.

There are exceptions to this rule - ‘=’ is the equality operator in SQL while it all other language its ‘==’. The assignment operator is ‘:=’ in pascal - in all other it is ‘=’. If you notice any difference in any operator write some code using that operator in this section.

Use if/else if/else

Create something like this pseudo code…

if (year > 2008) {
	print "Welcome to the future - yes we have flying cars!"
}
else if(year < 2008) {
	print "The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!"
}
else {
	print "Anything wrong with your time machine? You have not gone anywhere, kiddo."
}

In PHP, this section will look like this…

//If,else conditions
if ($year > 2008) {
	print "Welcome to the future - yes, we have flying cars!";
}
else if($year < 2008) {
	print "The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and decent, stay away from your parents!";
}
else {
	print "Anything wrong with your time machine? You have not gone anywhere, kiddo.";
}
print "\n";
	

If you use switch(and the new language supports switch), you can write an example of switch here.

Print ‘Hi there!’ 3 times using a for loop

A PHP example of using for loop(my all time favorite loop)…

// For loop
for($i=0; $i<3; $i++) {
	print "$i) Hi there!\n";
}
	
Create a list(array) and iterate through it using while/for/foreach loop.

I am using a while loop here - to keep things different…


//Numerical Array, While
$rules = array(
	'Do no harm',
	'Obey',
	'Continue Living'
);
$i = 0;
while($i<count($rules)) {
	print "Rule " . ($i+1) . " : " . $rules[$i] . “\n”;
	$i++;
}	
Create a hash(associative array) and iterate through it.

If the language you are going to learn have ‘foreach’, this maybe the best time to use it…

// Associated array, foreach
$associated = array(
	'hello'	=>	'world',
	'foo'	=>	'bar',
	'lorem'	=>	'ipsum'
);
foreach($associated as $key => $value) {
	print "$key: $value\n";
}
Create a function with an argument and a return.

If you need, you can create an example for call by reference, optional arguments, default argument value, variable argument count etc. as well - but they are not essential. Make sure you include the code to call a function as well.

// Function, argument, return, call
function hello($name) {
	return "Hello " . $name;
}
$hello_string = hello("Binny");

Optional Code

I don’t usually included some of these in my ‘hello world’ scripts - but they can be useful in some cases. Some may find them useful - so if you think you are going to use any of these, include it in your file.

Class, Objects, Member variables and functions

If you are a big fan of OOP, create the code for a class. Make sure it has all the following elements…

  • At least one member variable
  • At least two member functions
  • Access the member variable and the other function from within a member function.
  • Create an object of this class
  • Call a member function using the object.
// One for the OOP fanboys - Class, members, objects and stuff.
class Movie {
	public $name = '';
	public $rating = 0;

	function __construct($name) {
		$this->name = $name;
		$this->rateMovie();
	}
	function rateMovie() {
		$this->rating = (strlen($this->name) % 10) + 1; //IMDBs rating algorithm. True story!
	}

	function printMovieDetails() {
		print "Movie : {$this->name}\n";
		print "Rating : " . str_repeat('*', $this->rating) . "({$this->rating})\n\n";
	}
}
//Create the object
$ncfom = new Movie("New Country for Old Men"); //It's a sequel!
$ncfom->printMovieDetails();
File IO

Include this code - it is sure to come in useful someday. Unless you are learning JavaScript.

// File IO
// File reading, easy method...
$contents = file_get_contents('hello.php');
// Writing to a file
$file_handle = fopen(’/tmp/hello.txt’, ‘w’);
fputs($file_handle, “Hello World”);
fclose($file_handle);
	
Command Execution

I tend to include this - but it may be unnecessary for you.

// Command Executing
print `ls`; //Execute the command 'ls' and print its output
Regular Expressions

I always include them - I am a big fan of regular expressions. I have lost count of how many times they have saved the day.

// Regular Expressions
$string = "Hello World";
if(preg_match('/^Hell/', $string)) print "Yup - its evil\n";
print preg_replace('/l([^l])/’, “$1″, $string); //Remove an ‘l’ from both words. Should print ‘Helo Word’
	

Specialized Code

The language you learn may have a specialized field which we may have not used yet. If so, put it down here.

If you are learning javascript, write some code to access DOM nodes here. If you are using PHP, database connectivity will go well here. If you are learning any web server side languages, write the code to fetch the POST/GET request values. For PHP, it may look something like this…


// Database connectivity(native)
mysql_connect('localhost', 'root', '') or die("Cannot connect to the Database server");
mysql_select_db('Data') or die('Could not find a database called "Data"');

// Executing Query
$sql_handle = mysql_query('SELECT name,url,description FROM Comment LIMIT 1') or die('Query Error: ' . mysql_error());
while($row = mysql_fetch_assoc($sql_handle)) {
	print "Name	:	$row[name]\n”;
	print “URL	:	$row[url]\n”;
	print “Desc	:	$row[description]\n\n”;
}
// Just a note here - if you are going to use PHP with database in a production system(and you will, trust me), use a Database abstraction layer rather than the above mentioned native methods.

print $_REQUEST['username']; // Method to get the value of the field ‘username’ after a form submit. Will not work at CLI execution
	

Project

Next thing you need is a project - which you are going to build using the new language. Some points that you have to look for when choosing the new project…

  1. It should not be a huge project.
  2. At the same time, it should not be something trivial.
  3. There must be something about the project which would force you to finish it.

The third point needs a little explaining. You should not just pick any project that pops into your mind. It must be something that you need - something you could not live without. If it is important to you, you will have a motivation to finish the project. Otherwise, you will drop the project at the first sign of trouble. But if its important to you, you will spend more time on it - you will look at documentation, post the problem in forums, ask others in IRC channels. And eventually, you will fix the problem. And you will finish the project.

While working on the project, keep the ‘hello world’ open - you will find yourself referring to it often.

Alertle Launched

February 10th, 2008

Alertle Screenshot

I am one of the geeks behind Alertle. Its a web based RSS Reader. I am responsible for a good amount of the JavaScript areas of this app :-)

Features

Single Page Application
The entire application is contained in a single page - everything is done through Ajax. I will not advice that you make something like this(its a maintenance nightmare) but I can say one thing about it - its Cool. With a capital ‘C’.
Keyboard Shortcuts
I got the idea of creating a Keyboard Shortcut Library for JavaScript when I was working on this feature.
Autoplay
You can view articles as you are viewing a slideshow if you enable this.
Feedpacks
You can bunch a group of feeds together into a feedpack - and see all the posts from such a group together.
Sharing
You can share your feedpacks with other users

Perfect for High Volume Feeds

There is one major feature that sets Alertle appart from other RSS readers - it does not tell you if a post is read or not. Yeah, first you will think its a missing feature - but its not. I have used a lot of feed readers - once you subscribe to a couple of high volume feed - like say, BoingBoing or Slashdot or something, you can say goodbye to your sanity. It creates so many new items that the only way of staying away the mess is to click on the ‘Mark all as read’ button once every four seconds. You know what I mean - I am sure you have unsubscribed from many feeds for this reason.

With alertle, you can subscribe all these high volume feeds. And there is no pressure to view all the posts.

If you are an info junkie, I can guaranty that your will get lost for hours in Alertle.

Problems

IE is not Supported

We are still working on this - and due to deadline constraints, we decided to release Alertle without IE support. So if you are an IE user, I am sorry - but What in the World are you Doing? Ditch that terrible browser and get a real browser right now!

If we can get a few people to switch to firefox before we add support for IE, I will say that Alertle gone beyond and above the call of duty to make the web a better place! ;-)

Posts don’t have a Read Flag (pun unintended)

Um., yeah, I know - this is both an advantage and a disadvantage. This will prevent me from using Alertle for all my feeds. For my must-read feeds, I will still be using Google Reader. For the high volume stuff, I will use Alertle.

Getting to Know Alertle

So, what are you waiting for? Head over to Alertle and sign up for an account. Its FREE!

But if you are still unconvinced, here is a demo…

Links

BarCamp Kerala 2

February 3rd, 2008

Barcamp Logo

The second Kerala BarCamp was a success. The presentations were very informative and the people were interesting. What more do you want?

Sessions

Xtend IVR - A RAD Toolkit for Telephony

by Jayakrishnan K

This was a demo for an IVR toolkit called Xtend IVR. This is a fairly comprehensive IVR toolkit - it supports Indian languages and had its own simple scripting language. A demo is available here.

Mobile Social Networking in 25 days

By Vishnu Gopal

This was an introduction about how to create a web application. Its a real life case study of what went into building MobShare. Slides for this presentation is available at slideshare.

JavaScript: The most underestimated, undervalued and misunderstood language

By Your’s Truly

A introduction to the power of programming in JavaScript. Its a bit about the functional programming aspects of javascript and an appeal to start using for other things than validation. Slides are available online.

Develop a webservice in 5 minutes using nuSoap library

By Sanil

How to use the nuSoap PHP library to connect to and create Web Services.

Game Development Using Flash

By Eldhose, Juwal and Anish

This presentation was about the simplicity of creating a game in Flash. They also showed how to port the game into FlashLite so that it can be used in Mobiles. To know more, visit their website - CSharks (WARNING: Sounds)

The People

An advantage of such events are that you meet a lot of like minded people…

More Links about Kerala BarCamp 2

Subscribe to Feed