Title photo
frugal technology, simple living and guerrilla large-appliance repair
Mon, 01 Feb 2016

Learning more Perl for my Ode Counter add-in

I learn better, or should I say I only learn how to program when I have an actual problem to solve.

My current "problem" is figuring out how to generate more data out of my Ode blog's filesystem for my Ode Counter add-in.

I already report on the number of blog entries, how many are "real" entries and how many are Ode-generated social-media updates, plus how many images are in the filesystem and how many of those appear in actual blog posts.

Another thing I have wanted to do since I began using Ode was have the system generate a Categories/directories list in HTML for both a dedicated "site map" page as well as a sidebar display.


I'm using Perl's core module File::Find to, as its description says, "traverse a directory tree" and bring back information on what's in there. This works because Ode uses the OS' own filesystem to organize posts in nested categories.

The previous features of the Ode Counter add-in relied on numbers -- in this case numbers of files of various types -- and was based on creating numeric scalar variables.

But dealing with hundreds of individual directories to create a Category listing meant that scalar variables wouldn't do.

So I looked at hashes (key-value pairs) and arrays (collections of values).

Both are good candidates for handling the data for this part of the project, and I have been experimenting with each of them.

While something tells me that a hash would be a better choice, I've found some early success with an array to store the text of every subdirectory in my Ode /documents directory. What "sold" me on arrays over hashes at this point in the project was the ability to easily push a value into the array

I'm using a for (aka foreach) loop to continually iterate over the array and take the text of a directory listing and turn it into HTML that can be used by an Ode blog template.

And in iterating over the array, I'm using Perl's "general" (some call it "special") variable $_ along with the chomp command in a bunch of regexes to hammer the output of File::Find into usable HTML.

I almost forgot that I used Perl's grep to kill out array elements I don't want in the final output.

So in the interest of solving my problem, I learned about a dozen new things in Perl.

Why Perl? I know you're asking that question.

First of all, the Ode project is written in Perl, and its add-in system relies on Perl as well.

Perl excels when it comes to the manipulation of text, which is most of what is happening in every blogging application.

And many of these concepts -- variables, arrays, loops, regexes -- are fundamental to programming itself and are part of almost every language.

After the concepts themselves, it's all syntax and idioms. That's what I'm telling myself (and you).

Despite all the bad press it gets, Perl works. And it's fun to use (so far anyway).

Back to the Counter add-in. It's about to do more than counting, and might want to rename it. Right now I'm thinking Statistics or Stats.

The way I work on this add-in, I have two versions. I write the code and test it on a replica of my Ode /documents directory on my computer. That's where I work out all the Perl to get the output I want. This version of the file is awash in Print statements so I get output in the console when I run the script.

Once I have the script doing what I want with the local filesystem, I take the new code and deploy it to the actual add-in file, with all the code that Rob Reed has added to make the code work with an Ode site. Then I deploy to the server and see if it really works.

I'm still messing with the code, so I haven't gone from the local version to the Ode add-in version.

I've had a lot of time this weekend to work on the code, and while I'm worried that I won't have as much time in the near future, I don't want to rush the code from Stage 1 to 2. Part of that has to do with the HTML output. Right now it's certainly workable, but I'd like it to be better. The more I learn about regular expressions, the closer I can get. That's the theory, anyway.

The other thing that's holding me up is that I'm unsure about whether I can pass the contents of an array from the add-in to the HTML in the Ode template as easily as that of a scalar variable. I'm thinking the answer is yes. It will be easy enough to test. If it's not possible, I'll look into converting the contents of the array into a variable. Is that a thing in Perl? Turns out it is. You use join to do it.

Hey, I just learned another thing.

And it's all because I had a problem to solve (aka a burr in my saddle).