Tuesday, September 28, 2010

4 Perl concepts to scare off Beginners

Perl [5.8.6] is a relatively easier language to learn and program, but a tough one if you are trying to read someone's else code. The features in Perl programming language go well beyond, what a beginner is expected to know.

Typeglobs

Today, I just scared off a guy by throwing at him the concept of a typeglob. I conf'ess, that at the time of writing this blog, even I am not clear of the concept of typeglob's. But the point of the blog is to find concepts that Perl beginners don't care to learn about.
Typeglobs are Perl's internal datatype to hold entries in Symbol Tables. Better if you read it from perlmod.

Tie

Well, you can modify the behavior of a normal scalar, array, hash or file handle by using tie. Read more at perltie.

Closures

Not going into technical definition of closures, it's usage for generating iterators is interesting.

Here's a code to demonstrate the usage of closure for reading a tab separated file.

# Closure use case. Here $itr is our closure which will be used as an iterator.
# In Perl terminology, $itr is just a subroutine reference
my $itr = tabfile_itr($ARGV[0], "\t");
while (my $row = $itr->()) {
    # Let's say tabular file has three columns namely name, age and rank
    print "$row->{name} with age $row->{age} has a rank of $row->{rank}\n";
}

# Here's the closure factory. This function takes a file name and a separator
# and returns an iterator that gives a hash reference for each row.
sub tabfile_itr {
    my ($file, $seperator) = @_;
    # Open file
    open(my $fh, "<", $file) or die $!;
 
    # Get the header of file and split by separator
    my $hline = <$fh>;
    chomp $hline;
    my @headers = split $seperator, $hline;

    # Now this thing is called the closure
    my $closure =  sub {
        # The file handle "$fh" was lexically scoped but it will stay
        # statically scoped as long as the $closure is referenced
        my $line = <$fh>;
        chomp $line;
  
        # This is an important part, to break the recursion.
        return unless (defined $line);
  
        # Another lexically scoped variable used in this closure is
        # @headers. To understand each column in the file, we need to
        # have a record of @headers.
        my %col_hash;
        @col_hash{@headers} = split $seperator, $line;
        return \%col_hash;
    };
    return $closure;
}

Read more at perlfaq and perlsub. More closure as iterators examples at perl.com.

use vs require

This is an easy one. "use" runs in the BEGIN block and calls the import function of the package after "require"ing the package. Read more at perldoc -f use.

Tuesday, August 31, 2010

Sweet Python

My first impression of Python was language that was over-hyped. Now, that I have got to experience and feel it's power, I have begun to love it.

Why not semicolons, braces, dollars, at-the-rates and all the rubbish symbols that have been so long in other programming languages.
That's the first surprise you see, when you get to know Python. Well there may be many reasons like it keeps your fingers in the home row while typing or makes the code looks beautiful. My favorite reason is that it removes redundancy-why have a semicolon when you are going to have a new line anyway; why have pair of braces when you are going to indent and de-indent anyway. I have observed that it does reduces syntax errors, especially when you are away from your favorite on the fly syntax checking IDE.

What is module, function, class, method and object? they are simply types.

Wow! this is coolest feature of Python I have learned so far: Metaclasses. Everything you can see is a type, even in the language it self. And all the classes that we understand in the usual sense are just the predecessors of the type "object". Using these with setattr and getattr, you can generate any kind of code on the fly, even that one which in-turn generates code on the fly. Of course, don't do that because it do not generates a debugger on the fly.

Tuesday, January 5, 2010

LaTeX for formatting resume

I was looking for something that had macros for formatting my resume. LaTeX was the perfect answer to provide a consistent formatting. Here is my resume and it's tex and the original template page .


LaTeX
LaTeX is like typesetting system to produce standard printable documents like articles, books, reports and resumes.
As in HTML we just provide the content and external CSS files take care of formatting or display. On the same lines, in LaTeX we can provide the content and LaTeX styles or classes take care of the formatting and display.

If you want quick results, you can use an existing resume latex file and replace the contents. You can convert LaTeX to pdf by using online tools.

Getting Started:
http://www.latex-project.org/intro.html

Wiki book is quite helpfull:
en.wikibooks.org/wiki/LaTeX

Other recommended books:
  • A beginner's introduction to typesetting with LATEX
  • The Latex Companion

Thanks to the LaTeX resume templates (Top few google results for "latex resume"):
Use online LaTeX to PDF converter, in case you are not planning to install LaTeX.