Perl for dummies

In the hours of the early morning here at work i enjoy the silence. It allows my mind to unravel the craziness of the previous day and take quiet walks around the office space. Recently i have been noting the books that people have on their shelves, and its odd – many people have the O’Reilly Programming Perl novel sitting up on their desks/bookshelves here. I say this is odd because so many people dont know a god damned thing about perl or have a barely functional knowledge of how it works or how to code in it.

One such example is pertaining to a failed attempt to be a software developer. An application with a “Template engine” that is controlled by the function you run previously. This is one aspect of this application that should let you all know how complicated this has been devised. I am convinced that i could simplify the code base considerably, given the freedom to do so, but alas – i cannot.

Its odd that people purchase or borrow or share books when they are not willing to actually read them. There are a number of very interesting and necessary tidbits that sit just below the surface of Perl that are more than useful; that are more than powerful; that are able to correct code that you have just spent four hours working on instead of reading four hours yesterday. It’s called a Map. Use it.

This past weekend!

This past weekend was quite hectic. I went hiking, running, went to the mall, and… built out the basis of the new Atheist Nation Chat Engine. This project is my introductory project in use with JQuery publicly and I must say its been fun. The current chat engine has a few more features, but they are slowly being incorporated into the system and I think that it should be usable as a replacement in a couple days (assuming my workload doesn’t skyrocket again).

Code Example #5 – WinGrepReplace

In the day to day happenings of web development many of us get into binds and have problems getting out of them. Yesterday a good friend of mine came to me with a huge problem. Her website of more than 700 files needed to have certain entries replaced and the amount of time it would take to go through them all and replace the lines individually was not worth the effort. Thankfully she knows that i dont do anything that difficult by hand, i invent something to do the work for me and give my computer a smack on the butt and just wait for the results.

Here is the Perl code to do just that:

#!/usr/bin/perl -w
################################################################################
# WinGrepReplace
#
# Replace a given line in all files in a directory of files.
################################################################################

use File::Find;
use File::Copy;

die "Usage: $0 directory find [replace]"if (@ARGV > 3 || @ARGV < 2 ||
!-e $ARGV[0] || length $ARGV[1] == 0);

our ($dir, $find, $replace) = @ARGV;

find(&func, $dir);

sub func
{
   return if !stat || -d;

   open(FILE, "<$_");
   open(FILEO, ">$_.new");

   while (($line = ))
   {
       $line =~ s/$find/$replace/i if ($line =~ m/$find/i);
       print FILEO $line;
   }

   close FILE;
   close FILEO;

   move("$_.new", $_);
}

Although there are a couple of hacks and vulnerabilities in this block, it gets the job done nicely.