Making things easy through Perl

I want to be known as a social activist who uses Perl. Are there any in this doggone world? It sounds outlandish, yes, but that’s just me. Besides, there are two kinds of human beings in this planet: the freaks and those struggling not to be freaks.

My interest in Perl started when I was a budding Linux enthusiast in 2001. Although, the interest did not immediately morphe into an expertise because I was too tied to my work then. (I was in a work transition from IPD to PEACE.) I can’t imagine myself hacking on a terminal while I’m busying myself with writing terminal reports.

But I couldn’t resist it. Linux is to blame because with it goes my curiosity as to what makes the free operating system stick after all the years. Perhaps, my experience in VBA programming (Microsoft Access flavor) fed into my continued passion for tinkering with codes to automate things. But of all the programming languages, why Perl?

use heart;
my $learning = undef;
my @apps = ();
foreach (@apps) {$learning++;}

A typical programmer surely understands that poetic code. I found Perl cryptic at first. I felt like having to wander far to look for the treasure trove containing the secret to understanding Perl. But as soon as I started to use my heart (sounds corny, right?), and understand the fundamentals like the basic variable types (scalar, array, hash) and regular expressions, I began to feel like focusing more on my coding objective than being bogged down by my learning of the language.

Until of late, social activism and hacking have been two separate worlds. The two have crossed paths when the software development industry became highly commercialized and politicized and when the digital age was criticized for its contribution to social divides. Thus, the birth of the Richard M. Stallman-led free software movement and other free-software-inspired communities like Linux users groups, Open Source Initiative, Alliance for Progressive Communications, and Creative Commons.

I can say that I’m a manifestation of that phenomenon, modesty aside. I do activism (though no longer going to the streets as often as I used to) and use the ICT (basically gadgets and the Internet) to facilitate and spread that activism. But why in a hell am I using Perl?

I use Perl because it’s a great tool to process text data. I remember when I had to translate an NSO dataset (not in spreadsheet but in raw text format) into our database. The manual solution could be to print out the dataset and ask someone to encode it through a user interface. But I was emboldened by the fact that Perl is an expert in processing text files and producing reports from out of them. So I tried to learn Perl and create a script to do the task.

I also created a Perl script for my collection of words that I’ve learned. With it, I can add an entry and look in it for particular words I’d like to retrieve. Then, as I posted here, I created a script to automagically rearrange a list of disordered names according to lastnames.

The latest adventure I had with Perl was in relation to my blog. I wanted to keep a local database of all my blog entries. So I created a perl script to convert the XML-formatted backup to CSV, which I used to export the entries to my PostgreSQL-based database. Then I wanted to keep local log of visits to my blog entries, so that I can assess which entries are popular and have ideas on how to improve my blog. So I wrote a Perl script to do so. On daily basis, here’s what I do:

  1. Open the blog page showing entry hits on a particular day.
  2. Select and copy the list of entries with the corresponding numbers of hits.
  3. Paste the copied list into an editor buffer and save it.
  4. Run the Perl script, which asks for the date of the hits report. The local database is then updated.

Surely enough, Perl will be there to help me automate more things. I’m bent to use it not only for text-processing tasks but also for system administration and web programming. And I’ll not run out of a string of beads called Perl.

My own calendar app

First, I trusted jpilot as my PIM app of choice. It was because of the Palm 515 that I used to rely on. But when the gadget turned unusable (screen with scratches and easily-drained internal battery), I moved to Evolution Calendar to host my events and deadlines. But it was slow on my machine. Then I decided to go online all the way. I have used Google Calendar (for private and office-related events) and (for public gigs). But I cannot rely on the Internet all the way. And don’t ever think advising me to go back to the paper-based system.

Inspired by 30boxes and Google Calendar, I created an offline web-based calendar, merging the powers of PostgreSQL, Apache and PHP. I’m confident that my data are safe and are organized using the RDBM model. And it loads very fast with dillo.

I’m thinking of further improving the calendar app by integrating javascript and including a quick-add feature, which entails a code that perfectly parses the user input (much like Google Calendar and 30boxes). But those can wait. Meantime, I’m enjoying this app.

Got “1 rants” for 360

I’m not an English language expert. But I easily flare up, much less run amuck, when I come across simple grammar lapses. I can bear unintelligible English but I cannot take sitting down phrases like “1 comments” or “1 messages”.

The last time I checked (when I was a budding grade school pupil), a singular subject took a singular verb or something. In deference to 360 developers and programmers, and to all others taking this grammar lapse for granted, will you please insert just a line of code or two to handle correct English construction? For PHP buffs, it’s something like this:

if ($count[‘comment’] == 1) echo “1 comment”;
else echo $count[‘comment’] . ” comments”;

Powered by ScribeFire.

Mastering PHP: My calendar function

I know that there are already cool calendar PHP functions out there. But I decided to reinvent the wheel, for my own education. My calendar function

It took me a long while before I managed to make my calendar function work. Actually, the function is currently associated to my journal system. It’s relatively interactive in a sense that dates provide link to entries dated accordingly and links are provided to navigate to previous or next months.

In upgrading the function, I plan to include a text box wherein I can enter a month and year so that I can instantly move to that period. I also want to include a “home” link to get me back to the latest posts I have.

Mastering PHP: Handling spaces in passed values

When I try to pass to a separate web page a value that has a space, that called web page truncates the value from the instance of the space. Of course, the solution is to replace the space with “%20”. But the problem is that the value being passed comes from another variable (say from an item in a database table’s row). There must be a way to replace all instances of space in each variable’s value.

Solution: Use the str_replace function to replace all instances of space with ‘%20’.

A long leap from dBASE

dsc00600.jpgThis is my copy of the database programming “bible” I used to hold everywhere I went in the early 90s. I have designed over 10 database systems, complete with user interfaces, using dBASE III Plus. I did not go further to the IV series because I had moved to Microsoft Access by the time it was introduced.

The 10 systems I developed revolved around a) Library Document Encoding and Retrieval, b) Payroll System, c) News monitoring, and d) Human Resource Development.

I could have continued use of that DOS-based program had times not changed. By mid-90s, people went gung-ho over Windows 95 so yours truly had to change platforms correspondingly. The easiest I could do was to use Microsoft Access, which was very easy to learn. (Access is always touted as the database application of choice for the non-technical people (read: dummies).) From then on, database programming based on Access was my bread and butter.

By year 2000, I was transported to the world of free software–Linux. That opened my mind to other database programming possibilities. Thus, my current passion for PostgreSQL (also, but less, for MySQL). So database programming under Linux (as powered by PHP and Apache) is now my cup of tea.

It’s interesting to note how yours truly progressed from that lowly dot-prompt yet closed-source programming platform to the open-source web-based one. (Oh, but Oracle is absent from my profile. 😦 )

Mastering PHP: Fetching a PostgreSQL table and displaying it beautifully

The PHP Manual teaches the rudiment of getting a dataset from a PostgreSQL database. I followed it. Although it was successful, I was not satisfied with the way the dataset was presented over the web page: No information as to what the table is all about, no table heading, no border, the numbers were not aligned to the right, and no aggregate information (i.e., sumtotal). That gave me an overnight challenge (yes, I burned the midnight oil) to return the dataset with the problems cited solved. Success!

Look at the code I wrote: pgconnect2.pdf

Mastering PHP: Multiplication Table

I’ve been devoting time for my PHP learning since Saturday. Actually, one prerequisite of mastering PHP is that you should already have a flair for web publishing using HTML markups. If you’re a programmer (using C and Perl), learning PHP is much easier as it almost has similar syntax scheme like on control structures.

From now on, I’ll post all stuff I’ve discovered about PHP (which is a recursive acronym for PHP: Hypertext Preprocessor).

I made use of the recursive-function feature of PHP to produce a multiplication table at 10 by 10. The code I wrote looks like this:

Perl solution: Sorting disordered names

Problem: You got a list of names which start with firstname and which is disordered. Worse, each line starts with a string “> “.

Objective: Rearrange the names so that the lastname goes first with the indent string (”> “) gone.

Perl Solution:

#!/usr/bin/perl -w
# Context:
# The source file contains lines like these:
# > Dong Calmada
# > Zaldy Pineda
# > Jijo Fernandez III
# > Martin Howell Banares
# You want to sort the lines according to lastname, with the indent symbol gone.
use strict;

my $lname = “”;
my $fname = “”;
my @names = ();
my $name = “”;
my $name_compare = “”;

open SOURCE, “/path/to/source/file”;
open TARGET, “>/path/to/new/target/file”;

while (<source>) {
if (m/^\> ([a-zA-Z]+) ([a-zA-Z]+) ([a-zA-Z]+) ([a-zA-Z]+)$/) {
if ($4 eq ‘II’ | $4 eq ‘III’ | $4 eq ‘Jr’) {
$fname = $1 . ” ” . $2 . ” ” . $4;
$lname = $3;
} else {
$fname = $1 . ” ” . $2 . ” ” . $3;
$lname = $4;
} elsif (m/^\> ([a-zA-Z]+) ([a-zA-Z]+) ([a-zA-Z]+)$/) {
if ($3 eq ‘II’ | $3 eq ‘III’ | $3 eq ‘Jr’) {
$fname = $1 . ” ” . $3;
$lname = $2;
} else {
$fname = $1 . ” ” . $2;
$lname = $3;
} elsif (m/^\> ([a-zA-Z]+) ([a-zA-Z]+)$/) {
$fname = $1;
$lname = $2;
$name = $lname . “, ” . $fname;
push(@names, $name);

@names = sort @names;

foreach $name (@names) {
if ($name_compare ne $name) {
print TARGET $name . “\n”;
$name_compare = $name;

close SOURCE;
close TARGET;