Monday, August 24, 2009

And Now For Some Really Basic Perl - JSON Manipulation

And Now For Some Really Basic Perl - JSON Manipulation

In PerlWorld, there are basically two ways of doing virtually anything.
a) Do it yourself
b) Go look at CPAN and realize that Someone Has Done It Better
Being a card-carrying member of approach (b) above, here's a remarkably simple way of mainpulating JSON in PerlWorld

First, for some code based on Marc Lehmann's truly excellent JSON module

# Include the JSON module
use JSON::XS;

# Create a thing (called $json) that will be used to encode JSON strings
$json = JSON::XS->new();
# Make sure that the resultant JSON will print out all nice and pretty
$json->pretty(1);

# Create a basic JSON hash
$jsonStructure->{id} = "1234";
$jsonStructure->{method} = "ReallyCoolMethod";
$jsonStructure->{version} = "1.0.1";

# $prettyJSON will contain the text string which is the Oh-So-Pretty version of $jsonStructure
$prettyJson = $json->encode($jsonStructure);


If you printed out the contents of $prettyJson, you'd get the following
{
"version" : "1.0.1",
"method" : "ReallyCoolMethod",
"id" : "1234"
}

Likewise, if you just reverse the above process, i.e.,
$jsonStructure = $json->encode($prettyJson);
then you take a piece of JSON and convert it into a nice perl data structure.

Pretty trivial, isn't it?

Incidentally
1) Do note that the above is to make a point, and I've avoided so much that would go into making it Good. For example
a) using my --> my $json = JSON::XS->new();
b) using eval to trap errors
eval {
$prettyJson = $json->encode($jsonStructure);
};
if ($@) {
...some stuff here...
}

2) If you eliminate
$json->pretty(1);
above, you'll end up with output that looks like
{"version":"1.0.1","method":"ReallyCoolMethod","id":"1234"}

See the value of being pretty?

Sunday, August 23, 2009

ChatniPudi - The Universal Condiment

ChatniPudi - otherwise known as The Single Greatest Condiment Known To Man.

250 gms split Chana Dal
250 gms split Urad Dal
50 gms Red Chillies
50 gms Dried Tamarind from Block
2 cups Dried Coconut Flakes
1/2" side cube of Block Asafoetida, broken into two chunks
Vegetable/sunflower oil as needed in recipe

Break the tamarind into small strips or chunks. While doing so, pick out the seeds in the tamarind. Heat up an oven (or a toaster oven), to 250 degrees, and - placing the tamarind on some foil, roast it till it turns slightly crisp. You don't want it to char (or start smoking!), just lose most of its internal liquid. The amount of time it'll take totally depends on the water content of the tamarind.
Set a large (12"?) skillet on medium heat, add 1tbsp oil and add Chana Dal, one chunk of asafoetida and half the Red Chillies. Roast whilst stirring frequently till chana dal till it turns a fairly deep shade of brown. Not black. Brown. Black bad. Brown good. And yes, this is the boring part
Spread the roasted Chana Dal and chillies out on a sheet of foil, and let it cool. Remember that the Dal will continue to roast for a bit, so if you let it get too brown, it is going to end up black!
Repeat the above with the Urad Dal, the remaining asafoetida and the other half of the Chillies.
Put the coconut flakes in the skillet, and - stir constantly till it gets lightly warm. You don't want it to get brown, just slightly warm.

Once all the ingredients have cooled slightly (well, not cold. Cooled. Actually, still slightly warm) put small quantities of each of the above ingredients (some Chana Dal, some Urad Dal, some Chillies, etc.) into a mixer, and grind. Till when? Well, it really depends on your preference. Some people like their ChatniPudi to be totally smooth. Me, I prefer it with little Crunch Bits, so I grind it till it has a coarse gravelly consistency.
As each portion is ground, place in a large mixing bowl.
When it is all ground, add salt to taste - you'll probably need upwards of 2 tsp - and mix everything together quite thoroughly.
Let the whole thing sit for a few days, to let the flavours blend. Then enjoy with - well - everything.


Note:
  • All of the above ingredients should be obtainable in your local friendly Indian Grocery.
  • The red chillies used above are of the Wrinkled south-indian kind, called 'Bydagi'. When I say 'wrinkled', I mean wrinkled. The wrinklage is pretty obvious. Not creased, not dented. Wrinkled. If you can't find the wrinkled kind, use the normal red-chillies, but use only a third as much (16gms). Do not use X-Hot chillies. And yes, i mean you, Mr. I like my food hot. Trust me on this.
  • And now for the tricky part - you need a good blender. Ideally, you have a mixie, but on the off-chance that you don't, any blender will do. The tricky part, however, is that you want a blender whose blades pretty much go to the very edge of the bowl. You're typical Hamilton blender has blades that still have a gap between the blade and the bowl. If you add too much of the ingredients to this type of blender, you'll end up with nothing grinding. Really.
    So, your choices are to either (a) Get a mixie, (b) Use a typical blender, and grind Very Small Quantities at a time or (c) Use a coffee grinder, and grind Even Smaller Quantities at a time