Category Perl

AJAX Picture Gallery using jQuery View Comments

Oct23

I have been longing for a custom picture gallery for quite some time, to showcase my pictures, but did not want to go for a ready made solution like Coppermine or Gallery. Sure these are great products with endless features, but would not give me that MADE BY MYSELF feeling :-) So I decided to tinker with Prototype and Script.aculo.us , to see if any good carousel is available which I can use as an image slider. I had previously used YUI Carousel by Bill Scott of Yahoo! but did not want to use it because it was way too heavy and hence slow to load initially. So I decided to take a look into jCarousel by Sorgalla which is based on the excellent jQuery Javascript framework. Both jQuery and jCarousel are very lightweight, feature packed, customisable, and contain almost all, if not all, the features of prototype and scriptaculous.

So after a little toying with the carousel and learning a few cool effects of jQuery , I made up http://gallery.naveeng.com . Right now it is very very basic and it’s development is still under progress. I will be adding a lot more features to it, which include:

  • Multiple Albums
  •  Admin Panel
  • User Comments
  • Ratings
  • Last Added Pictures
  • Random Pictures
  • Tags

It may take a lot of time but one day all the above features (plus a lot more!) will be there for sure! The gallery has a lot of jQuery JS code, with the image details stored in XML format on the server. The server side scripting and image manipulation are done in Perl using the fantastic PerlMagick API of ImageMagick .

Perl one-liner to sort a hash and return the top most key/value View Comments

Oct17

A while ago Deepz asked me whether we can sort a hash and print the top most key or value or both in a single line, in PERL. After a little permutation & combination, I wrote the following piece. I’m sure there must be a more elegant and efficient way of doing it, but this will also serve the purpose on most occasions and with a little tweaking can be applied to almost all hashes.

  • The code needs to be modified if the hash keys contain any digits
  • The sort algorithm can be changed as per the requirement, by default I’m sorting by hash values which are digits

print grep {s/\d//g} grep {/1/} map { $i+=1 , $_.=$i , “\n”} sort {$hash{$a} <=> $hash{$b}} keys %hash;

Install Perl Modules without root View Comments

Oct16

How many times Perl developers have to scratch their heads when they find that there is a module dependency which is breaking their code. Worse, they do not have sufficient privileges to install the required modules on the system. And the worst – there is no immediate way to get the module installed and the application needs to be tested/deployed ASAP! Well, I faced a similar situation during my experience in deploying a website on a shared hosting account, where I was on the mercy of the Hosting Service Provider to decide which modules I’m going to use and which not. But this is hardly the way a programmer works, and more so for a Perl programmer! I came across a few wonderful workarounds (hacks!) to overcome the above.

Download the module from CPAN or elsewhere. Place it in any directory, where you have permissions. Preferably, create a directory called

lib

in the application directory and place your module there. Untar it, you will see a few files and folders – Makefile.pl (to install the module, we don’t need this) , Readme.txt (this either), Examples (not required), lib directory – this is the only thing we are concerned with. It contains the main module files. Go inside the lib directory, and copy all the folders (if any) and all .pm files and place them directly inside the lib directory you just created. Remove all other files and folders.

The next thing which you need to do is go to the Perl code where you want to use that module and write the following:

BEGIN { unshift @INC, "./lib"; }
use Module::Name;

And voila!! Your code works like a charm with all the functionalities of the module included.

How it works? - When you use a module in a program, like

use CGI::Ajax;

perl searches for it in a predefined library search path on the system. You can determine which directories are in the path by executing

perl -e 'print for @INC;'

Any module in order to be usable in the program needs to be in any of the directories above, which is where the modules get installed if installed via

cpan or ppm

command or using Makefile.pl.

But in the situation discussed, it is not the case, as we cannot install a module as it should be. Instead what we do is place the modules in a directory of our choice (lib) and then insert the path of that directory in the

@INC

so that later when the perl interpreter tries to do

use Module::Name;

it finds its path in

@INC

Why BEGIN { } ? -

use Module::Name

happens at compile time, when the interpreter reads the program while

unshift @INC, "./lib"

happens at run-time, after the compilation, i.e, attempt to load the module, even if the latter is placed above the former in the program. So in order to force the program to modify

@INC

before trying to use the module, we use

BEGIN { }

block. Any code which is written within

BEGIN { }

block is executed at compile-time as opposed to other code which is executed at run-time.

Note: Remember that many modules have further dependencies, i.e. modules which are required for the former to work. And if these dependencies are not on the system, the code won’t work. You need to perform the above operations for each of those dependencies as well. For example, if CGI::Ajax requires Class::Accessor to work, you need to

a) create the directory CGI and place Ajax.pm within it

b) create the directory Class and place Accessor.pm and the directory Accessor within it and

c) unshift/push the path of the directory which contains these 2 directories CGI and Class, into @INC.

I have used this method to use more than 30 odd modules and have faced no difficulty whatsoever. However it will not work if the module’s Makefile.pl does more than just copying the .pm files in the right place and do some other work such as using external C libraries etc.

Another method of extending

@INC

is setting the

PERL5LIB

environment variable on your system to include the path of the manually placed modules.

In Bourne Shell do something like-

PERL5LIB=/path/to/module/directory; export PERL5LIB

It is useful when you do no want to write the BEGIN blocks in all the files where you use the module. But only users who are setting the

PERL5LIB

environment variable can only use it and other users can’t.

My First JAPH! View Comments

Oct15

Finally after procrastinating for long, I decided to tread the path traversed only by the GODs of PERL – writing JAPHs!! And my brain storming for about 6 hours finally ended with a JAPH, not too bad for a debut, albeit a little amateur. For those of who are still wondering what the heck is a JAPH – it refers to a PERL program which prints the phrase JUST ANOTHER PERL HACKER. It is an art initiated by Randal Schwartz and further mastered by the likes of Abigail. My piece of code is as follows:

#!/usr/bin/perl

@b=map +{keys %$_} , map +{chr}, grep {!/6[68]|7[0137]|81/} (65..85);

%hs1=(74,1,85,2,83,3,84,4);
%hs2=(65,1,78,2,79,3,84,4,72,5,69,6,82,7);
%hs3=(80,1,69,2,82,3,76,4);
%hs4=(72,1,65,2,67,3,75,4,69,5,82,6);

for $i(1..4) { $n=”h2″.$i;for (0..13) {@abc=keys %{$b[$_]};$k=pop @abc;map { ${$n}{$_}++ } keys %{$b[$_]} if ((($i==1) && ($k=~m![tsuj]!i)) || (($i==2) && ($k=~m![rehtona]!i)) || (($i==3) && ($k=~m![lrep]!i)) || (($i==4) && ($k=~m![rekcah]!i))); }}

for (1..4){$m=”h2″.$_;$ms=”hs”.$_; print sort {${$ms}{ord $a} <=> ${$ms}{ord $b}} keys %{$m}, ” “;}

I particularly don’t like two parts of the above code -

1) The 4 hashes at the top – they are used to sort the letters in the words (It took most of my time)

2) The big if ( ) { } block – it kinda gives the code a very amateurish look.

So while I work on improving myself, do give in some suggestions if you may please :-)

And if you think that was smart, then have a look at http://www.cpan.org/misc/japh or http://en.wikipedia.org/wiki/JAPH for some of the most astonishing piece of coding across any language on this planet.

WebOSS ’07 – Web and Open Source Conference in Kolkata View Comments

Oct15

Finally, the day arrived this Saturday, October the 13th, when the first Web & Open Source Technology Conference took place in Kolkata. It was a great initiative to bring like-minded people on the same platform and share our views on all things Web, OSS and Technology. Since the venue host was WBUT , a large percentage of the audience were IT and Comp. Sci. students of the university who were eager to get a peek into the REAL things, and learn the tricks of the trade from the people in the Industry.

Pradeep, Myself and Shabbir Bhai

The auditorium was good enough with a capacity of around 150, well-airconditioned and the stage space more than enough. The LCD projector could have been more to the center of the stage, rather than just behind the dais, with speaker coming right in between the screen and audience. The only and a very big disappointment from the arrangements point of view was the absence of microphone!! Huh! A seminar without a microphone! Well, the equipments were there but the keys to them were with a gentleman who preferred skipping the college that day and was untraceable!

So after lot of failed efforts by the volunteers to get the microphone working – they even brought another one, but it didn’t work – the event got underway at 11:30 AM, 1 hour behind schedule. Hussain was the first one to start off on RIA – Rich Internet Applications. It was a great insight on the Web 2.0 client side technologies which are riding most of the snazzy websites today and threw light on AJAX, Flex, Flash , OpenLaszlo among others. His thoughts were well received by the audience with quite a few questions popping up on the subjects.

Hussain on RIA

Next came Pradeep with a talk on what is AJAX and how to implement it in Web Apps. He showed some nifty piece of code on how to create xmlHTTPRequest and fetch data from the server in asynchronous mode. Also included were examples on how to use JSON instead of xmlHTTPRequest to implement AJAX. The content of his presentation is available at http://www.go4expert.com/forums/showthread.php?t=6829&referrerid=146 and can be downloaded from http://www.slideshare.net/s_pradeep/building-applications-using-ajax

Pradeep on AJAX

Shabbir bhai resumed the proceedings after a well deserved lunch break with his expertise on optimising web pages, and gave a lengthy (detailed) talk on Speeding up your Web Pages. He covered topics such as Database Design, Query Optimization, Web Server & Programming tips and tweaks among others.Although his soft voice was finding it hard to make up for the lack of mic, he too put up a great show.

Shabbir Bhai on ‘Speeding up your Web Pages’

I was next on the block. With fears of rushing too fast and getting over with my stuff in 10-15 minutes, I started off on Web Development in Perl. Since most of the audience were students with little or no previous exposure to Web Development, Programming and Perl, added to the fact that previous sessions were a bit too technical for most of them, I had to make it as simplistic as possible. So I stressed most on the introduction part which dealt with explaining the Web Development cycle, internet in general and programming. Yet, technicalities about Perl needed to be covered as they were a part of my presentation and most importantly, were the focal point of the show! After around 30 minutes Hussu started prompting me that we were running out of time and I needed to pack up fast! So I hurried with the last part of the presentation by going through the points of difference between Perl and PHP.

Myself in Action!

Ab to samjah jao bhai!! :P

The presentation can be downloaded from http://www.slideshare.net/naveeng/web-development-in-perl/

Naveen's Journal is powered by WordPress and FREEmium Theme.
developed by Dariusz Siedlecki and brought to you by FreebiesDock.com