Why does a Laptop Battery come half-charged?

July 23rd, 2010 by Rohan

I recently purchased a Laptop after a break of nearly a year. I’ll always be a desktop guy, but you can’t beat the benefits of having a laptop.

Anyway, I noticed that the battery pack which comes with the Laptop is never fully charged. I remember this was identical to the IBM ThinkPad I had bought five years back. Do you wonder why? I mean, why the batteries don’t come in fully charged? I’ve got a clue.

I guess that the company which ships the batteries out save electricity by not fully charging them. Plain and simple. Take a few Kilowatt hour per battery, multiply by the millions which ship out, and you’ve got a considerable cost saving reason to not fully charge the battery.

I’m sure this is with all devices that come with batteries. Your cell phone, the iPod, iPhone, you name it.

[Post to Twitter] Tweet This Post  [Post to Facebook] Facebook 

VIM Tip: Restoring a Session

November 30th, 2009 by Rohan

If you use VIM’s sessions feature, to restore a session, you can just type

$ vim -S

This will look for the Session.vim file in the current directory and load the previously saved session.

[Post to Twitter] Tweet This Post  [Post to Facebook] Facebook 

Get started with jQuery, AJAX and JSON in your Perl web applications

May 22nd, 2009 by Rohan

In this post I’ll go over a simple example on how to use jQuery with AJAX and JSON with your Perl web applications. I’ve used a plain-old CGI script along with Template Toolkit for this. However, you can very well use this under any web application framework. The main point is to get your concepts clear about how it all works.

First the demo - View it here

This example might not seem relevant to real-world applications, but its a good start to jQuery and JSON. Of course, you need to have some experience with jQuery. Don’t worry though, jQuery is amazingly simple to use once you get started on it. It does bite your ass couple of times, but once you understand the language, you’ll not be able to do without it.

This is the source code for the HTML template.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>Perl, jQuery and JSON</title>

<script src="jquery-1.3.2.js"></script>
<link rel="stylesheet" type="text/css" media="all"
    href="s.css" />

</head>

<body>

<div id="no_ajax">

    <p>This form does not use AJAX/JSON. The submit will do a full page
    refresh.
    </p>

    <form method="POST">
        <button type="submit" name="submit_form" value="1">Get User Details</button>

    </form>

      [% IF user_details %]
          <h3>User Details</h3>
          Name: [% user_details.first_name _ ' ' _ user_details.last_name %]<br/>
          Sex: [% user_details.sex %]<br/>
          Age: [% user_details.age %]<br/>
      [% END %]
</div>

<div id="use_ajax">

      <p>This form uses jQuery. The submit will send a GET AJAX request and
      will return a JSON object which we can use to update the div.
      </p>

      <button id="submit_form_ajax">Get User Details</button>
      <span id="ajax_spinner" style="display: none;">
          <img src="spinner.gif"/></span>

      <div id="display_user_details">

      </div>
</div>

<script>

// show the spinner for AJAX requests
$("#ajax_spinner").ajaxStart(function(){
    $(this).show();
});
$("#ajax_spinner").ajaxStop(function(){
    $(this).hide();
});

$("#submit_form_ajax").click(function(){

    $("div#display_user_details").html('');

    $.getJSON('?ajax=1', function(result){
        $("div#display_user_details").html(
            '<h3>User Details</h3>'
            + 'Name: ' + result.first_name + ' ' + result.last_name + '<br/>'
            + 'Sex: ' + result.sex + '<br/>'
            + 'Age: ' + result.age + '<br/>'
        );
    });
});

</script>

</body>
</html>

In lines 15-32 I define a form which will send a plain-old CGI request to the Perl script. The CGI script then does its stuff and returns the user details as a template variable which we then print out.

In lines 34-48 I define a second form (well, not really a form) which has just a single button. This button’s click event will be used to send an AJAX request using jQuery.

Now the fun part! Lines 52-58 set it up so that we can see a spinner image whenever any AJAX requests are sent.

In lines 60-72, we define a function for the click event of the button. In this function we call the getJSON jQuery method. The first parameter is the URL to which we append the ‘ajax=1′ query parameter, so the back-end CGI script knows that this is an AJAX request to be processed. The second argument to getJSON is a function which receives the JSON data sent back by the CGI script as its first argument. So, result is a javascript object which you can use like any other javascript object. I use the result object to populate the display_user_details div with the user info.

Now the Perl CGI script


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

  use JSON;
  use Template;
  use CGI;

  my $q  = CGI->new;
  my $tt = Template->new(
      INCLUDE_PATH => '.',    # Path to template files
      PRE_CHOMP    => 0,
      POST_CHOMP   => 0,
  );
  my $tt_params;

  # check form submit
  if ( $q->param('submit_form') )
  {
      my $user_details = get_user_details();
      $tt_params->{user_details} = $user_details;
  }

  # check if its an AJAX submit
  if ( $q->param('ajax') ) {
      my $user_details = get_user_details();
      my $json = to_json($user_details);
      print $q->header('application/json');
      print $json;
      exit;
  }

  print $q->header('text/html');
  $tt->process( 'user_details.tt', $tt_params );

  sub get_user_details
  {
      # pretend like we're doing some heavy task!
      sleep 1;

      my $user_details = {
          first_name => 'Bob',
          last_name  => 'Green',
          sex        => 'M',
          age        => 35,
      };
    return $user_details;
  }

If you’re familar with Template and CGI, this should be pretty simple to you.

The JSON stuff is in lines 25-31. get_user_details() returns a hashref which we convert to JSON data using to_json(). We first tell the browser that we’re sending JSON data in line 28, and then we send the JSON data. I’m using exit cause this is really a simple (not real world) example! In real world code, you would have all AJAX requests processed by a separate module so you can set the HTTP response headers in there, etc.

Again, do check out the demo, and hope you can now get started with using jQuery and JSON in your Perl web applications. Myself has a long way to go, but jQuery has really helped me a lot on the AJAX road.

Links

  1. jQuery
  2. JSON on CPAN
  3. Template Toolkit on CPAN
  4. [Post to Twitter] Tweet This Post  [Post to Facebook] Facebook 

Perl with XP - Extreme Perl

May 16th, 2009 by Rohan

In spite of programming in Perl for the past eight years, I’m not sure how I missed this book.
Extreme Programming in Perl is a wonderful book which teaches you about XP with a focus on Perl. I came upon this website during some Google searches I ran for a Perl programming problem I was facing. I really wish I had come upon this book a few years earlier, but no complaints!

The author does a superb job of keeping the narrative real. The book explains the four core values of XP: Communication, Simplicity, Feedback and Courage in an easy-to-digest manner and does not stray away with high or dense ideals. So yes, it does feel like you’re getting instruction, but thats the best way IMO. I remember a short story by Stephen King about a young boy and his Grand Father who is terminal. The boy asks for some advice about life and stuff. The old man looks at him and says - “I don’t give advice, only instruction”. Beautiful!

Adopting XP can be a problem which should be dealt with delicately though. From what I’ve digested so far, I think having a strong functional and unit test suite is a nice way to begin.

Another gem would be what the author says about Simplicity - do the absolute minimum necessary to solve the problem first, and then the design actually begins when you start to refactor the current code into the codebase.

[Post to Twitter] Tweet This Post  [Post to Facebook] Facebook 

Moose with Rose::DB::Object. Roose? Morose?

May 7th, 2009 by Rohan

Yeah, I know it sounds silly. I’ve now read the Rose::DB::Object tutorial and the Moose manual several times and can’t wait to get started on my current and future projects using these technologies.

I find Rose::DB::Object to be a refreshing change over all the ORMs out there, and I especially like the concept of Manager classes. The docs are quite well written with several practical examples and its a good read to review your knowledge of database design too.

Moose is all the hype these days. Its so easy to get started using it and again - the manual is well written.

So I’m wondering how I will use these two modules together to get a persistent object system in my applications. From my searches, I found that there are examples of where people have used Moose and DBIx::Class together, but I couldn’t find samples with Rose::DB::Object. Anyway, I’ve decided to tackle this myself.

[Post to Twitter] Tweet This Post  [Post to Facebook] Facebook 

Perl one liner to display module version info

April 29th, 2009 by Rohan

I use this Perl one-liner to display the version number of a module. This module should be available in your @INC paths.

If I need to know the version of CGI.pm

$ perl -MCGI -e ‘print “$CGI::VERSION\n”‘
3.29

I created a shell script so that I can just type the module name.

$ cat pm_version.sh
#!/bin/sh
MODULE=$1
perl -M$MODULE -e ‘print “$’$MODULE’::VERSION\n”‘

So now, I can do this.

$ pm_version.sh Google::Adwords
v1.13

[Post to Twitter] Tweet This Post  [Post to Facebook] Facebook 

My new Line 6 POD XT

April 28th, 2009 by Rohan

After much deliberation and budget crunching I finally made up my mind and bought a Line6 POD XT around a week back.

And you can read more about it here - POD XT

To be honest, I’ve not really dived into it yet, but I’m more than pleased with what this processor can offer. Recording guitar sounds into the computer works amazingly well, and the POD XT is perfect for starting with a home studio.

The manual is superb! It reads like a novel. Grab a beer and you’re set. It has good humour thrown in too. The guys/gals at Line6 are definitely guitar/sound maniacs and the manual is testament to this fact.

Although its a bit expensive, its well worth the investment. Considering all the amp models and effects are modeled in software, they do provide patches and more amp models which you can buy.

My conclusions?
1) The POD XT is a marvel of electrical, sound and software engineering
2) Line6 is great at product marketing!

[Post to Twitter] Tweet This Post  [Post to Facebook] Facebook 

Use Thunderbird to increase your email productivity

February 16th, 2009 by Rohan

Email has now become the tool of choice for quick and effective business communication. From the biggest companies to the smallest start-up, we all use email daily to communicate with Our Clients as well as for internal team communication. Although email does lack the collaborative feature, its the best tool for one-to-one communication. Did I say it lacked the collaborative feature? My bad! Mailing lists are definitely how a group can exchange thoughts among its members.

In this blog post, I’ll share with you how I use Thunderbird to improve my email productivity. We’ll start off with a fictitious project titled projectX with a fictitious client The Client. My objective is to have all email communication related to this project to be kept under one single folder. I hate using the Sent folder to look back on what I had written before. We’ll also use Threaded View for displaying the list of messages.

The people involved are:

  • Rohan Almeida - almeida@aarohan.biz
  • The Client - rohan@almeida.in

The first thing I’ll do is create a project folder under Inbox


The next step would be creating message filters so Thunderbird can correctly sort the incoming email into this folder. Before we do that, I first use a neat little trick so that all my outgoing emails are sent back to me (via a bcc), so I can filter these into the projectX folder.

For this, go to Edit > Account Settings > Your Account > Copies & Folders

All outgoing emails are now sent back to me! You can opt not to save a copy in the Sent folder if you would like to save space, since we will have them in the projectX folder anyway.


On to the message filters now. Go to Tools > Message Filters, and create a new filter as shown below.

This filter sets up Thunderbird so that all incoming email from the almeida.in domain (The Client’s domain) is set to be moved to the projectX folder. Cool! Now we also need a filter for my outgoing emails.

This filter sets up Thunderbird so that all incoming email with a To header containing the almeida.in domain (The Client’s domain) is set to be moved to the projectX folder. Incoming email? To header? Yes. Recollect the Bcc step? So when I send an email to the client, this email is sent back to me and has a To header containing The Client’s email domain. So Thunderbird moves this into the projectX folder. Very cool!


Lets now look like at an email discussion between me and The Client. I start off with a query.


Thanks to the bcc and the outgoing filter setup, this email now lands into the projectX folder.


The Client replies. Make sure you have enabled the Threaded View in Thunderbird.


A couple of emails later.




Notice how emails with a different subject are automatically grouped under a single thread? Doesn’t this view give you a better picture of your email conversations?

Also since the filters are applied to a domain, all emails related to the Client’s company will get filtered into the projextX folder. Hmmm.. what if I now want to separate multiple projects of the same Client? I’ve found that I’m not in need of this right now, as the curernt method still offers me much more control over the communication than without threads, and this would be a task for another day. :)

[Post to Twitter] Tweet This Post  [Post to Facebook] Facebook 


Tweet This Post links powered by Tweet This v1.4.1, a WordPress plugin for Twitter.