Archive for April, 2008

Hello Script for Tcl

Thursday, 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

Thursday, 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

Thursday, 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.

Subscribe to Feed