April 30, 2007
Random entry
For many many months now, the random link at the top of this page just below the banner hasn't been working. I decided to fix it once and for all.
First of all I redefined the link to refer to a new CGI script mt_random.pl which I wrote myself using good old Perl. Here is how the new link is defined:
<a href="/cgi-bin/mt_random.pl?blog_id=1" title="Jump to a completely random place on this blog...">Random</a>
Of course the whole random magic of the situation is the Perl script mt_random.pl itself, which looks like this:
#!/usr/bin/perl -w
use DBI;
use CGI;
use strict;
my $q = new CGI;
my $format_url = "http://www.kiffinsblog.com/archives/%d/%0.2d/%s.html";
my $hostname = 'localhost';
my $database_name = 'db_name';
my $user = 'db_usr';
my $password = 'db_password';
my $dsn = "dbi:mysql:database=$database_name;host=$hostname";
my $blog_id = $q->param('blog_id') || 1;
my $dbh = DBI->connect($dsn, $user, $password) or die "Can't connect to $dsn: $DBI::errstr";
my $sql = <<EOF;
SELECT
entry_basename,
entry_title,
date_format( entry_created_on, '%Y' ) AS entry_year,
date_format( entry_created_on, '%m' ) AS entry_month
FROM
mt_entry,
mt_author
WHERE
entry_blog_id = $blog_id
AND
entry_status = 2
AND
author_id = entry_author_id
ORDER BY rand( )
LIMIT 1
EOF
my $sth = $dbh->prepare($sql);
$sth->execute();
my $ref = $sth->fetchrow_hashref;
print $q->redirect(sprintf($format_url, $ref->{entry_year}, $ref->{entry_month}, $ref->{entry_basename}));
You can now try it out yourself by clicking on the link above, and if you cannot find it then clicking here.
Pretty amazingly magical don't you think?

Posted at 5:24 PM | Permalink | Comments (0)
September 1, 2006
Format it whatever
$_=1234567.7891;
s/\B(?=(...)*\.)/ /g;
print;

Posted at 10:45 PM | Permalink | Comments (0) | TrackBack
June 20, 2006
Server name not in referer
How to distinguish comment and/or email spam from the real thing:
#!/usr/bin/perl
use strict;
use CGI qw( :standard );
my $server_name = server_name();
my $referer = referer();
my $is_spam = $referer !~ /$server_name/;

Posted at 5:06 PM | Permalink | Comments (2) | TrackBack
February 18, 2006
Javascript anyone
Hey, did you know that there was a Javascript extension to the Template-Toolkit?
That's exactly what Jemplate does, and it does it quite elegantly I must admit.
This CPAN module is definately worth looking into, just for fun at least. For more information you might want to check out the article Jemplate - A Template Toolkit for Javascript.
Closer inspection of the code reveals that this is nothing more than a quick switch Template::Directive sleight of hand, that's all really.
You might also want to consider using OpenThought - Web Application Environment which doesn't require page reloads.
Not so sure if I will ever use it, but it is interesting to know about anyway.

Posted at 5:49 PM | Permalink | Comments (0) | TrackBack
February 17, 2006
Nameless scalars
Unlike the special constructs which make creating references to anonymous hashes and arrays as easy as pie, there is unfortunately no easy-and-quick way in Perl to create so-called nameless scalars. Kind of a bummer.
Can this be viewed as a serious shortcoming of Perl? Could be or not.
Well there is still hope. Instead you will have to do something like the following:
my $scalar_ref = \do{ my $anon_scalar = 'what the heck' };
Now it is up to us Perl folks to figure out what this actaully means and how one should go about using it effectively (or not).
Borrowed from one of my favorite Perl books which is called 'Perl Best Practices' by Damian Conway.

Posted at 4:20 PM | Permalink | Comments (0) | TrackBack
January 29, 2006
Amazing discovery
This is a truly amazing discovery for me.
As it turns out, it is possible to debug Perl threads, forked processes as well as CGI programs. You can even debug modules which have been preloaded, inspect object-oriented Perl stuff and even verify user-defined regular expressions on the fly.
Just to think of all those late night debugging episodes doing it my boring way when I could have saved hours and hours (if not days) by doing it the right Perl way.
To think that I was so naive as to believe I knew most there was to know about Perl.
This fine book (Pro Perl Debugging) has turned out to be a real eye-opener for me.
While not for the light-hearted, any true Perl person worth his or her weight in salt will definitely have to read this book, which I recommend highly.

Posted at 8:52 PM | Permalink | Comments (4) | TrackBack
December 7, 2005
Perling in Amsterdam
Yesterday evening I attended the Amsterdam Perl Mongers meeting for the very first time.
I was the only one without a laptop, so I had to sit patiently at the table twiddling my thumbs while the gang discussed those perl things we all love and cherish.
This computer language called Perl is quite unique when it can inspire so many folks to come together and talk about it once a month.
Next month I will be sure to bring my laptop with me so that I can melt better into the group. Can't wait.

Posted at 4:17 PM | Permalink | Comments (4) | TrackBack
November 2, 2005
Random string
Speaking of elegant ways to get something complicated done with a minimum amount of good-old perling, here's an interesting algorithm:
sub random_string
{
my $max = shift;
return join '', map { ('a'...'z', '0'...'9', 'A'..'Z', '0'...'9')[rand 72] } (1..$max);
}
In case you couldn't figure it out yourself, it takes a single integer as input and generates a string of that many characters made up of random upper- and lowercase letters and digits.

Posted at 8:21 PM | Permalink | Comments (6) | TrackBack
October 31, 2005
Slicing hashes
An elegant way to mix and match hash entries is by using the technique of hash slices. For example, let's just say that you have defined the following hash:
my $config = {
firstname => 'Joseph',
lastname => 'Smith',
user => 'pickle',
...
database => 'fruits',
password => 'z4KLf3&3z',
...
host => 'zeta.demon.nl',
};
Suppose that you are only interested in the hash-keys related to the database stuff. Then you can use the following fancy one-liner:
my ($dbhost, $dbdatabase, $dbuser, $dbpassword) =
@$config{ qw( host database user password ) };
As the saying goes, there is always more than one way to do it with Perl. Pretty neat stuff, wouldn't you say?

Posted at 1:12 PM | Permalink | Comments (1) | TrackBack
October 23, 2005
Template toolkit
For the third time now in less than a year, I am rereading that fantastic Perl Template Toolkit book.
Not only is this a great read and each time around I discover new and interesting tidbits I had missed before, the framework allow a robust plugin design allowing one easily to extend the tool in advanced ways.
Here it is in a nutshell
- Define the framework from the requirements
- Place the front-end on top of this
- Implement the processing between elements
- Define the underlying interface to drive these processes
- Snap in the various plugins to achieve this
- Test it all based on the original requirements
For more information about this great tool, you might want to check out the official template toolkit website.
Have fun.

Posted at 9:28 AM | Permalink | Comments (0) | TrackBack
October 21, 2005
Geocoder
Hey Geo::Coder::US is here, and you can even use the native SOAP extension if you want to be really cool.
Build Your Own Geocoding Solution with Geo::Coder::US.

Posted at 9:44 PM | Permalink | Comments (0) | TrackBack
October 12, 2005
Thanks amazon
The postman came by again and brought me two more books that I ordered some time ago:
- Perl & XML
- Programming Web Services with Perl
Thanks alot Amazon.co.uk!

Posted at 7:39 PM | Permalink | Comments (0) | TrackBack
October 6, 2005
Encapsulated cleverness
Is there anyone out there who can tell me what the following piece of code actually does?
@list_out = keys %{ {map {$_=>1} @list_in} };
If so then please leave a comment if you dare. I am willing to bet a good many euros that not a single person out there knows the answer to this abstract perlishness.

Posted at 6:40 AM | Permalink | Comments (5) | TrackBack
August 31, 2005
Simple slurping
This is more than likely the best way to read an entire file into a given variable $code all in one go:
my $code = do { local $/; <$fh> };
Notice that by localizing the input record separator $/ variable within the brackets, unwanted side-effects are avoided.
Of course, if one is more prone to power slurping, then the module Perl6::Slurp offers a much more advanced interface, but one will just have to be patient and wait awhile.

Posted at 10:51 PM | Permalink | Comments (0) | TrackBack
August 25, 2005
One direction or the other
"Then again, almost every maintainability issue is, by itself, marginal. It's only collectively that subtleties, clevernesses, and esoterica begin to sabotage comprehensibility. And it's only collectively that obviousness, straightforwardness, and conformity to standards can help to enhance it. Every samll choice when coding contributes in one direction or the other."
Taken from Perl Best Practices, Damian Conway (p. 165).
In otherwords, when it comes to good old down-to-earth programming, let's not be too clever and just stick to the point. The well-defined point that is.

Posted at 2:43 PM | Permalink | Comments (0) | TrackBack
August 24, 2005
Apply transform
Here's an interesting and very efficient way to transform each list element, returning the list of those modified copies:
my @nice_words =
apply { s/$EXPLETIVE/[DELETED]/gxms } @words;
That is of course if you are willing to use the not-so well known List::MoreUtils CPAN module.

Posted at 4:49 PM | Permalink | Comments (0) | TrackBack
January 23, 2005
Deep recursion
All of a sudden my browser froze up and nothing happened anymore. As it turns out, I had unknowingly introduced a nasty bug into the fancy web application I was working on. This resulted in the dreaded 'deep recursion' error which can be triggered to raise its ugly head by something as innocent looking as this:
sub show_bugs (
my $recurse = shift;
# do something
show_bugs(1) if !$recurse;
}
As you can see here, it is important that one NOT forget the !$recurse or it's dooms day all over again. Be careful, it's a dangerous and unpredictable world out there.

Posted at 1:40 PM | Permalink | Comments (0)
June 24, 2004
MediaWiki and more
This MediaWiki thing could be the way to go when it comes to communicating in a pseudo-structured endless manner. You might want to have a look at the Wikipedia (an open-content encyclopedia in many languages), Wiktionary (a collaborative project to produce a free multilingual dictionary in every language) or Wikiquote (a free online compendium of quotations in every language) and more for a quick(?) glimpse into the endless possibilities of this intertwined medium. Pretty amazing stuff, that's for sure.

Posted at 10:06 PM | Permalink | Comments (0)
April 21, 2004
Get the job done
"In the end, it is the developers themselves who hold the project together. Each individual bears their own share of the responsibility for finding a task that suits their skills, coordinating with others to keep duplicated effort minimal, and making sure the job gets done."
- Perl 6 Essentials, Chapter 1.

Posted at 12:01 AM | Permalink | Comments (0)
April 16, 2004
Obfuscated code
For true-blue Perl geeks like myself, the Perl Monks web site is an interesting place which is really worth a regular visit. They have this section called Obfuscated code in which you can discover amazing little jewel snippets. Like this one, for example:
#!/usr/bin/perl
*AUTOLOAD = *_;
sub _ { /:+/; $, .= "$' " }
print Hacker(Perl(another(&Just)));
So what does it do? How does it work? What does it mean?
You now have two choices: 1/ Take the challenge, figure it out yourself, and verify you have the correct answer by clicking the button below, or 2/ Be impatient, immoral, and outcasted from the Perl community by hitting the answer button without raising a pinkie.

Posted at 9:52 AM | Permalink | Comments (0)
April 1, 2004
A bunch of children
Okay so I have got the main parent process up and running just fine. Now it is time to fork off the child processes, opening up the two-way pipes so that they can all communicate with each other. The only problem now is the synchronization which still has a number of slight wrinkles that require a good dose of ironing out. Sometimes it works just fine, but during unpredictable moments it clogs up, resulting in processes freezing, reading/writing becoming blocked, and the whole gateway hanging in thin air. Time to roll up my sleeves and look deeper, much deeper that is.

Posted at 1:26 PM | Permalink | Comments (0)
March 21, 2004
Unblessed again
So I thought that this weekend I could delve somewhat into the fine art of object-oriented Perl, create my own fancy objects and all. But I keep getting the following aggravating error:
"Can't call method "_init" on unblessed reference at Logger.pm line 8."
What the heck does that mean?

Posted at 6:31 PM | Permalink | Comments (0)
March 9, 2004
Thanks to Perl
I am now a full-fledged Perl programmer with a real life assignment about which I feel happy and relieved. Looks like alot of fun as well as a challenging project during which I can hone my programming skills in Perl even more and hopefully impress folks with my abilities so that they will either want to keep me on or decide to put me on another relevent assignment. Thanks alot Perl for making my life that much better.

Posted at 8:29 PM | Permalink | Comments (1)
March 6, 2004
Random button
Just for the heck of it, I decided to stick a so-called random button on this blog to give folks a chance to jump around this blog in a more precarious and unexpected way.
If you look closely, you will see the bluish button on the right margin, and placed randomly (note the meta pun) all over the place. The button looks like this:
So how does it work? you might be wondering. Well for those Perl gurus out there it is nothing more than a simple script which scans a given directory, grabs a random HTML-file from the listing and does a good old redirect via the HTTP-header.
Of course, in order for this little code snippet to work, you have to have CGI enabled on your web server, e.g. included as part of your subscription.
Confused? Then here is a little code which I hope helps you folks out:
#!/usr/bin/perl -w
#
# rand_redirect.pl
#
# Redirect for MovableType where a user will be sent
# to a random blog entry, category or whatever the
# HTML-file is present in the archive directory.
#
use strict;
my $dir = "archives";
my $rootdir = "/Blogger";
use CGI;
my $q = new CGI;
if (opendir(DX, $dir))
{
my @allfiles = sort grep { $_ ne '.' and $_ ne '..'
and ($_ =~ /\.html$/i) } readdir DX;
closedir DX;
my $num = (@allfiles);
if ($num > 0)
{
my $n = rand($num);
my $fname = $allfiles[$n];
print $q->redirect("$rootdir/$dir/$fname");
}
}
The last step is the easiest. Decide where on the page you want the button to appear, and just add the following code there:
<input type="button" value="Random" onclick="document.location='/cgi-bin/rand_redirect.pl'">
Hope this little code snippet helps someone out there whenever and however that may ever be. Please tell me what you think.

Posted at 6:04 PM | Permalink | Comments (0)
June 24, 2003
Display images directory
Now if you click on the random image up on the top left-hand corner of this web page, you will be shown a list of the images directory.
I found this little jewel of a Perl script via the CGI Resource Newsletter which advertised the free script called WebImageLister.
Give it a try and find out for yourself.

Posted at 2:09 PM | Permalink | Comments (1)
May 26, 2003
Commify text
Here's a little Perl snippet which will place commas in any number string right where they belong:
sub commify
{
my $text = reverse $_[0];
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
return scalar reverse $text;
}
So for example, by calling commify("13245766") the value "13,245,766" will be returned.
All the little commas right exactly at the correct positions using a simple regular expression. Pretty amazing language that Perl is. Does it make sense?

Posted at 9:47 AM | Permalink | Comments (2)
May 14, 2003
Scraping the internet
Alright so I admit it, I have been a naughty boy lately. Desperate to find customers, one often resorts to conniving means, seedy methods bordering on the immoral. Oh well.You see, I figured why pay one of those companies out there on the Internet tons of bucks for email address lists, or pay them even more to broadcast mailings all over the world when I can do that myself.
I am a pretty smart guy, sometimes. At least that is what I fool myself into believing.
So this is what I did. I wrote a subscription mailing list at GishTeq which allows potential customers to sign up for mailings and newsletters. Later, one may even modify personal settings or opt-out altogether by unsubscribing.
Now the trick is getting people to sign up. Why don't I scan the Internet, collect emails and sign them up myself? When they get the first mailings, they can always unsubscribe.
I created another Perl script which can scan certain sites and scrape off the emails. If possible, it can even log in to subscriber lists and automatically scan these web pages also.
All I do is give the script a site url, start it up and there it goes. Ten minutes later or so I've got my list formatted as CSV so that I can even import it into an Excel sheet for future reference.
You probably won't believe me either if I tell you that with this technique I have successfully harvested more than one thousand warm leads.
Here's a hint (if you know Perl). Use the use LWP::UserAgent module to get the contents of the page and then scan it. See anything interesting, scan deeper via the internal links. Poke around and see if you can find anything interesting. Is there a user id that is passed around? Try all values of that user id from say 1 to 1000 and collect the results.
Sorry, I've been a bad boy. But I want to become famous also.

Posted at 4:23 PM | Permalink | Comments (2)
April 17, 2003
Sitemap using Perl
Here's something clever I thought up the other day and decided to use on my new GishTeq website to spruce things up some.What it does is generate a bulleted list of all the web pages as links in alphabetical order according to the title. Just add the EXEC-include (SSI) statement wherever and load it up for the surprise.
An example of it in action can be seen at the GishTeq sitemap page. Hope you like it. Feel free to use it as you see fit.

Posted at 8:16 PM | Permalink | Comments (3)
December 19, 2002
Random images
In case you haven't noticed it yet, I have added yet another new and exciting feature to my weblog. You see, along with the hundreds of other things I am trying to learn these days, Perl is one of the more difficult. Wouldn't it be nice if I could generate a random image? I am certain that everyone reading this entry would certainly agree that such added functionality would improve the quality of this site immensely. Perhaps attracting even more droves of visitors. So that is what I did. In order to see it, just look up at the upper left hand corner of this page and you should see it. Each time you visit or hit the refresh button, the image of the future famous blogger will change.Here is the script for all you fine folks out there who know a little bit about the wonderful world of Perl programming:
In order to use this, your web host will need to support SSI and the Perl mod Image:: needs to be installed. Feel free to use this at your own risk and good luck.

Posted at 10:43 AM | Permalink | Comments (2)
October 3, 2002
Ordered books
Since my birthday is coming up soon, I decided to order two more books from Amazon in the hopes that my shipment will arrive in time for the big event. They've got less than nine days, but so far I have never had to wait more than a week. Now I will tell you which two books I ordered if you promise not to make fun of me. The titles are:
- The Java Programming Language by Ken Arnold et al.
- Effective Java by Josh Bloch.
I figured that I have pretty much learned enough about HTML, Javascript, XML, CSS, Perl and CGI that it is time to take the next jump into the most obvious domain about which I need (would like) to be more knowledgeable. I have heard so much about the wonderful world of Java and at the same time know so very little about it. Time to expand my knowledge another slight click. Is this a sign of getting senile, crazy, too old for this stuff or am I simply a born techie guru type at heart? Take your pick. Any way, the books will add a pleasant counterweight to whatever it is I am planning to do in the not so near future.

Posted at 12:55 PM | Permalink | Comments (2)
May 16, 2002
Perl woes
So I thought I would get fancy again and make my guestbook even nicer, nicer than it was ever meant to be. However, after I had changed some of the perl stuff and uploaded it to my cgi-bin directory again it all went boom. That is if I tried to add a new entry to the guestbook and hit the good old submit button, an ugly 500 internal error flew back in my face. Ugh and oops and what? What was the problem exactly? The most obvious choice is to recheck the Perl script for possible syntax errors, but that was perfectly fine. I had only changed a few items (5), and they all looked perfectly healthy to me. When I stuck back the original cgi-script to the server, everything was okay again. Weird. Why did I have to go off and change things, try to make them better but make them worse in the end? As it turned out, the cause of my problems was this and has to do with me "trying" to transfer my development environment to my new machine. The usual hassle. I use the wonderful tool called LeechFTP (sometimes crashes and/or generates cryptic error messages in German but in general pretty dependable and I am used to it). There is this option for setting file extensions which should be automatically uploaded in ASCII-mode. Well, you guessed it. The list of extensions was reset to the default after installation (".txt.htm.html.nfo.diz") and does NOT include the CGI extension. Oops, that means that when I try to transfer the file guest.cgi to my site, it gets sent in BINAIRY mode which is bad. Bad boy. Bummer man. I now quickly fixed that by appending the text ".pl.cgi.htaccess.pm.cfg.cgi" to the end of the default list. Don't want to forget the other types of files I upload regularly and end up having the very same frustrating problem now do I? Now everything works just fine again. Again and again. No use wasting energy doing low-level debugging stuff when that is not necessary. First think, think deeply, before proceeeding to the next step. Again and again.

Posted at 9:40 PM | Permalink | Comments (2)
February 23, 2002
Infinite knowledge
Right now I am reading a bunch of books simultaneously in my never-ending attempt to acquire infinite knowledge. Nine books all in a row, to be more precise. Depending on my mood at a given instant, I will jump from the one book to the other. Sometimes smack dab in the middle of a chapter, I will close the book I am currently reading and open up another book. Some reading sessions consist of switching books five or six times an hour. I suffer from an insecure feeling that perhaps I am not increasing my knowledge as productively as I should. That what I am reading at the moment is not valuable enough in the sense that I am suffering an acute shortage by not improving my life sufficiently and quickly enough! This can be very frustrating. The innocent pleasures of reading suffer in that regard which is ironic to say the least. There is this endless source of interesting information out there, and there is absolutely no way I can absorb it all in the limited span of my lifetime. By nature I have an addictive tendency, a proclivity which bends me in fixations. Especially when it is in any way remotely related to the esoteric acquisition of knowledge. So why bother in the first place? Infinite knowledge is preferable, of course. Unfortunately one cannot attain this by reading, even if one could have enough time to read every single book in every possible language that has ever been written in the history of civilization. In order to give my readers a hint of how far along this reading adventure I have come, here is a list of books that currently lie on the reading table next to my bedside. The order is from bottom to top, or since I am by nature symetrical and neat, from the widest (usually the thickest, but now always) in decreasing order:
- Programming Perl, Third Edition by Larry Wall et al.
- Javascript, The Definitive Guide by David Flanagan.
- The 7 Habits of Highly Effective People by Stephen R. Covey.
- Essential CSS & DHTML for Web Professionals by Dan Livingston.
- Web Design in a Nutshell by Jennifer Niederst.
- Cascading Style Sheets 2.0 Programmer's Reference by Eric A. Meyer.
- A Beautiful Mind by Sylvia Nasar.
- The Power of Now by Eckhart Tolle.
- Caesar by Colleen McCullough.
As you can see from the list I am pretty much obsessed with Web-related stuff at the moment. However, I try my best to balance this with other non-Web stuff. Reading one book at a time would probably be much more efficient, but I am too impatient. For the sake of simplicity, let's just say that I am about half-way along the pursuit of infinite knowledge.

Posted at 12:08 PM | Permalink | Comments (6)








