Tag Archives: IRC

More on my latent love for Perl and why I find it difficult to adopt Python(2.x)

I was lurking about on IRC (irc.freenode.net, #cbstream, #crunchbang, #crunchbang-fr, #gull) and someone presented a small bot in Python to do some actions the official chan bot wasn’t doing, such as punching people, handing out coffe, brewing tea or, more importantly, doing forum/wiki searches.

I asked to be given a copy of the code, and was quite happy to see it. Given the structure of the code, it was fairly easy to add basic functions:

self.plugins = ["&help", "&list_plugins", "&recherche", "&search", "&volka", "&coffee", "&punch", "&tea", "&kiss", "&ff"]

It was fairly easy to add functions such as “&tea”, or “&ff”, given it was essentially a copy-pasta party.  However, all &ff did was search 2 forums and a wiki for 1 keyword and print out the resulting links to the irc channel.

That’s all good and nice, but what happens when you want to look in one place specifically? What happens if I need to search for a particular author? The code needs to be easily re-factored.

The first step was to separate the command in to a  ‘command – argument -options’ syntax. So instead of calling &ff. you would call &search my key words wfe (w searches a wiki, f a french forum and e the english forum).

This however, did not quite go as expected:

if ((re.match("^&recherche", message)) or (re.match("^&search", message))):
  if nombreArg >= 3 :
    if re.match("w", arguments[-1]):
    if re.match("f", arguments[-1]):
    if re.match("e", arguments[-1]):

The only problem with this, is that depending on how the options you give it, it will only call the first one. i.e. if you call ef, it will do(something(f)) and break, instead of then doing the instructions for (e). this of course wouldn’t such a big deal, if it weren’t for the fact that both the english and french forums allow for by_author searches.

This is what that function would be in perl:

sub search{
 my $options = pop @_; #may need a split as it's a bot, it #will be receiving $message and not an array
  if ($_[0] eq '-a')
    my @authors;
    shift; #removes the -a
    while ($_[0] ne '-k')
         { push @authors, shift;}
    shift; #get rid of the -k
shift if $_[0] eq '-k';
my $keywords = shift;
while (@_)
  {$keywords .= "+" . shift;}

Of course, this is only the begining of the  function, however, as it stands, it is already working better than it’s current Python counterpart. I hope never to have a project to write froms cratch in Python 2.x.