A few bad days

Standard

“Choose life. Choose a job. Choose a career. Choose a family. Choose a fucking big television. Choose washing machines, cars, compact disc players and electrical tin openers. Choose good health, low cholesterol and dental insurance. Choose a starter home. Choose your friends. Choose leisure wear and matching luggage. Choose a three-piece suit on a hire purchase in a range of fucking fabrics. Choose D.I.Y and wondering who the fuck you are on a Sunday morning. Choose sitting, watching mind-numbing spirit crushing game shows stuffing fucking junk food into your mouth. Choose rotting away at the end of it, pissing your last in a miserable home. An embarrassment to the selfish, fucked up brats you have spawned to replace yourselves. Choose your future. Choose life.”

Well, that is from the opening scene of the best movie ever made: Trainspotting. That being said, the last few days have been an utter devastation to the above “regime”. So I took a detour from my usual ramblings and started working on a Perl project called “Complain management system”. It’s not a big fucking deal really, but this is my first commercial project in Perl and it is quite simple. To keep things simple and clutter free (this is not a tutorial…), please refer to this tutorial for details.

The requirements are as follows:

  • Perl. On Linux systems, you must have it installed already. If not, use your favorite package manager (I use apt) and install it. On Windows, go for Strawberry perl.
  • Catalyst. Catalyst is an agile MVC framework written in Perl. It is very robust and increases the development speed considerably. You can use CPAN to install it.

I’ll assume that you already are a Perl programmer because I cannot really get into the intricacies as such. I’ll directly start with how to use the Catalyst framework to make a small web application. The source code will be upped on git.

Now exactly like Mark Renton, I chose not to choose life. I chose something else. And again like Mark Renton, there were no reasons. Who needs reasons anyway when you got weed? The month that started with a good bright fucking smile on my face ended with me fallen on the freakin’ road about to be bowled over by a double fucking decker bus.

“Goa! Dude I have 5 days off and I’m not fucking them up eating at the miserable mess food you love to stuff your mouth with!” said my friend. So we went to Kashid. One of the best trips of my life. Starting with the ferry ride from Gateway of India, the journey was memorable… weed, beer, beach, silver sand, moonlit night, high tide and the cool nightly sea breeze. The water was fresh and sparkling right until the moment I lost my phone.

So starting with starting to write a web application in Perl. I’ll be using Linux obviously so all you Windows lovers are on your own. Open your terminals and fire off:

code-ninja@GeniuSisMe:~/Projects$ catalyst.pl MyApp

This will dump a long output which will result into a directory being made in your PWD (mine is Projects) by the name “MyApp”. When you change into the MyApp directory, you’ll see that there are some sub-directories and some Perl scripts. The “lib” directory will contain your code, the “root” directory has all the HTML code, the “script” directory has generated scripts to help you test and develop your application.

MVC is an acronym for Model View Controller architecture. You can read about it on Wiki. Essentially, it is a paradigm which divides your projects into three modules namely Model, View and the Controller. Model is the database, View is what you see in the browser and Controller is the bond between View and the Model. Lets make a Catalyst Controller. Fire off:

code-ninja@GeniuSisMe:~/Projects/MyApp$ perl script/myapp_create.pl controller Complain

It will again dump some output which essentially will generate a template controller for you to play with. Open “MyApp/lib/Controller/Complain.pm” in your favorite text editor. You’ll see just one sub-routine (“index”):

sub index :Path :Args(0) {
    my ( $self, $c ) = @_;

    $c->response->body('Matched Complain::Controller::Complain in Complain.');
}

This is supposed to be programmed by the developer. You can add more complex routines and stuff you want to do with the data.

But no! I will not give up! It was just a phone I lost! For a brief moment, I ate like a fucking pig and worked out at the gym. Went out and got fucked up without having any fucking idea about my miserable and depleting funds

“Lets go out and eat!” said Dang. I acceded. The Baghdadi restaurant in South Mumbai is awesome. Order a Chicken Curry and the guys will serve you with a whole chicken floating in a decadent curry. Whole… full, not cut. One big fucking piece of the meat you love to devour. “Lets go to Malabar hills and smoke up!” said Sammy. So we went to Malabar hills and smoked up. Came back… there is only 100 bucks in my account and there are 2 full weeks before I get my salary and I live in Mumbai. Fuck.

And so started the miserable week of utter penury. No food, no outing, no beer and only weed. No fucking idea about how I’d manage the 2 weeks, I give up eating like a fucking pig and working out at the gym. I give up taking a bus to work and started walking 3kms to reach my office. No money means no food during lunch time… not even a freakin’ banana.

So lets see… Now that we know how to make a controller, we will see how to make a model and a view. We write a small SQL script to make a database. Note that I’m using the SQLite3 database and the commands I fire will be specific to it.

We make a table in our database which will store user’s complain amongst other things.

PRAGMA foreign_keys = ON;

CREATE TABLE complain (
    complainID INTEGER PRIMARY KEY,
    userName TEXT,
    userAddress TEXT,
    userContact TEXT,
    productSerial INTEGER,
    productType INTEGER,
    productMake TEXT,
    complain TEXT,
    status INTEGER -- 0: open, 1: processing, 2: closed
);
INSERT INTO complain VALUES(1, 'Code Ninja', 'Andheri', '98111111111', '0112358', 'Laptop', 'HP', 'Screen Crapped up!', 0);

save it under any name you want (I called it complain.sql). Then give it to your database engine so that it creates a table.

code-ninja@GeniuSisMe:~/Projects/Complain$ sqlite3 complain.db < complain.sql
code-ninja@GeniuSisMe:~/Projects/Complain$ sqlite3 complain.db
sqlite> select * from complain;
/* whatever output */

To enable the model, we say:

code-ninja@GeniuSisMe:~/Projects/Complain$ perl script/complain_create.pl model DB DBIC::Schema Complain::Schema create=static dbi:SQLite:complain.db on_connect_do="PRAGMA foreign_keys = ON"

Don’t worry about the complex commands, you don’t really need to know them. I know what each of the commands mean but it really doesn’t matter if you don’t. Now, if you look in the “lib” directory of your project, you can see a new sub-directory called Model. Open the Perl module you see inside it. It has a Perl representation of the data columns you just made in your SQL script.

The mess where I eat refused to give me any credits because I was already in debt to them for 20 freakin’ bucks. But I disabused this nice cigarette shop guy from not giving me credit and coaxed him for my constant supply of tetra-pack milkshake and cigarettes. So, no phone, no money, one milkshake and cigarettes to go through the day. Ripped my landlady off of 500 bucks (never to be returned) and some more out of my colleagues at work. They are too friendly and don’t expect things back… good for me.

Under such conditions, the grass was a luring distraction. When you are on junk, you have only one worry… scoring. When you are off it, you are suddenly obliged to give a fuck about all kinds of things. Got no money, got no girl… about human relationships that don’t really matter. So I gave in to my senses and rolled a joint and took a long fucking drag.

And that long fucking drag could not satiate my will of fucking myself up. Its never enough… you must go out and fuck everything all over again. You know you cannot afford it but you cannot also ignore the craving. To top it, the attachment, the feelings I had for the one girl I fell for became overwhelming… I missed her, way too much. So I get higher than usual and planned to go out (knowing that I haven’t got any money). Got on the wrong bus and I stepped out of it in the wrong way… against the direction of motion. Slipped and the road was thick with traffic as it was peak traffic hours. Saw death, right in front of me… a red double decker.

For the view, we will need a few more things. First we will need a view controller that will to and fro the data between Model and the HTML web page. Second, we will need a page template that will be rendered as an HTML page by the Catalyst engine. For the template, we will use the Template Toolkit along with jqDock and a bit of jQuery. To make a view controller, fire off:

code-ninja@GeniuSisMe:~/Projects/MyApp$ perl script/myapp_create.pl view TT TT

This will create Template Toolkit view that the Catalyst engine renders into a complete HTML page. It will also create another sub-directory under “lib” called “View”. You must follow that tutorial I linked above because I’m not writing a fucking tutorial. I’m just elucidating what all is there to make a small web application in Perl.

And in this situation, the quote “A friend in need is a friend indeed”, was tested. Found a gem :). Thanks to the guy that I was able to survive this state of penury without affecting all my debauchery. Now, I have got money… got a bonus from work (for my work with this application) and now…

“The truth is, I’m a bad person, but that’s gonna change. I’m gonna change. I’m cleaning up and moving on. Going straight and choosing life. I’m looking forward to it already. I’m going to be just like you. The job, the family, the fucking big television, the washing machine, the compact disc players and electrical tin openers, good health, low cholesterol, dental insurance, mortgage, starter home, leisure wear, luggage, three-piece suit, D.I.Y, game shows, junk food, children, walks in the park, good at golf, washing the car, choice of sweaters, family christmas, indexed pension, tax exemptions, clearing gutters, getting by, looking ahead, till the day you die!”

The code can be found here. I haven’t uploaded the SQL script I used but you can make it yourself. The README has everything you need to know about how to run the application.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s