| Subcribe via RSS

Change on the (Distant?) Horizon

January 8th, 2017 | No Comments | Posted in Meta-Posts

Well, I’m still not sure if this means I’m returning to regular blogging or not. Perhaps only semi-regular activity is the best I’ll be able to manage. But for now, if I can manage to at least write once a week it should keep the Perl Ironman Challenge bot happy.

I’m going to be changing some things around here, if I do in fact start spending time here on a regular basis again. The first thing I’m going to work on changing is the theme. I actually like the current theme, as I’m a fairly minimalist type when it comes to web design. But it isn’t set up to handle mobile devices very well, and that’s just a reality that every blog needs to embrace if it hasn’t already (and I haven’t). In a more perfect universe, I’d have both the time and the know-how to either hack this theme for better mobile support, or just roll my own theme. But I don’t have that know-how (or time), and to be honest I’m not in a hurry to learn WP theme development. But if you read this and prefer mobile devices, rest assured that making the site mobile-friendly is the top priority. Well, trying to write consistently is technically the top priority, but this runs a very close second.

Next, I’m going to do something about the near-astronomical number of spam accounts that have registered here (and continue to do so daily). I direct the mail notifications for this site to my Gmail address, and I do not exaggerate when I say that I average 100 or more new users per day. Of course, they’re (almost) all bots. I have a plugin in place that protects the site against login attacks, but I don’t have anything in place really that tries to prevent bots from signing up. That will have to change; I’m kind of tired of having over 100 new emails from WordPress daily… it makes accessing my Gmail account over IMAP a pain in the ass. (Yeah, I can filter them to a folder, but that doesn’t really solve the problem, it only masks it.)

(A side effect of the registration work is that it might impact legitimate registered users. I would prefer to purge the database of the fake users, but I’m not sure if I’ll be able to without putting real accounts at risk in the process. If I do have to just blow away the whole database, I’ll warn users here on the blog at least a month or so in advance.)

Following that (or perhaps in concert with that effort) I’m going to re-visit the other plugins that I’m using here, and try to trim away those that aren’t really contributing anything useful and possibly add a few new ones that do. While I’m not much of a cheerleader for PHP, I do have a great appreciate for the flexibility and ease-of-use that WordPress provides. It has a hell of an ecosystem behind it, and I need to be taking more advantage of that.

In terms of content (presuming I do start writing more regularly), expect to see less of a focus on Perl. I’m not giving up on it by any measure (Perl has been very good to me over the years), but I don’t want to get typecast as only a Perl programmer. During the first half of last year, I started really seriously working on learning Clojure, which is just a really fun language to program in. (Well, assuming you like functional programming and aren’t irrationally allergic to Lisp.) So far I’ve mostly just been working through problems on Project Euler. I can’t seem to link directly to my “progress” page, but my user-name on PE is also rjray. So far, I’ve solved 87 problems and earned 6 of the problem-solving awards (haven’t gotten any posts in the forum to be made permanent yet, so no forum-post awards). It’s been quite interesting, but by it’s very definition PE is all about the math, so this hasn’t really exposed me to the full range of of Clojure’s capabilities yet. I hope this year to move beyond just PE and do some other things with Clojure that will push me to fill in the missing pieces that I haven’t yet used.

In that same vein, I am hoping to spend a little time dusting off my long-dormant C/C++ skills. I now work for NVIDIA, who is doing just amazing things with deep learning, machine learning, etc. using their GPU technology and the CUDA platform. And CUDA is pretty much all about C++ (well, there is also support in Fortran and Python, to be fair). My C++ is so rusty, that when I last used C++ they (the standards committee) were only just considering templates as a new feature, and hadn’t quite agreed on a standard yet. My role at NVIDIA doesn’t directly involve C++ (or else I’d be in a pretty bad spot in terms of being able to do my job), but CUDA and GPU programming look fascinating to me, so I’m hoping I can at least dip my toe in the water at some point in 2017.

And lastly (for now), you may even see some Python content here. While my NVIDIA role is primarily Perl, there is also a fair amount of Python floating around, and I’ve been slowly acquainting myself with it for the last couple of months in order to understand, enhance and fix things. But don’t expect to see me become a Python convert; most of what I have seen has me scratching my head at how the Python community can really feel so smugly superior to the Perl community. Most specifically, I’ve been listening to Pythonistas brag for YEARS about how much better the object model in Python is when compared to Perl’s. Having gotten a taste of Python’s object model, I can only assume I’m missing something big. Because I’m pretty far from impressed so far.

But that’s a rant for another time.

Tags: , , ,

I Made a New Thing in Perl

January 1st, 2017 | 2 Comments | Posted in CPAN, Perl, Software

(For now, I’m going to casually ignore the fact that it has been literally two years since my last blog posting. If this post heralds a return to regular blogging, I may address that in a future post.)

I made a new thing in Perl: YASF, or Yet Another String Formatter. I’m rather happy with it, even though I suspect that no one else is using it yet. I’m happy mostly because it’s the first new module idea that I’ve been able to actually turn into real code, in several years. The “why” of this is somewhat complicated, but can best be summed up in six words: Deep depression, self doubt, strong medications.

So what is it, and what does it do?

It’s a lightweight string-formatter module inspired by the format method of strings in Python, with a little syntactic sugar based on Python’s % operator for string formatting. It scratches a particular itch I’ve had for some time now: why do I keep writing (basically) the same hash-based search-and-replace regular expressions over and over? By which I mean something like:

my $pattern =
    'This is a string with one {key1} followed by another {key2}.';
my $hash = { key1 => 'value', key2 => 'different value' };

(my $string = $pattern) =~ s/{(\w+)}/$hash->{$1}/ge;

Now, this is a fairly convoluted example, but it gets at the point: this is a very common pattern. And it is usually not this straightforward; you probably have a series of value-sets that you would want to substitute into $pattern in order to get a new $string value, otherwise you would just use the hash values directly when declaring $pattern. Maybe your pattern is a global value made read-only by Readonly or Const::Fast. There are (I imagine) more reasons for wanting to do this than I can think of by myself. But I had already run into enough reasons on my own that I wanted to do something about it.

Coincidentally, around the most-recent time that I found myself writing something for this pattern yet again I was also learning Python for a task I was given at my day-job. I was looking at an existing code base and tracking down the elements and constructs that I didn’t immediately recognize in O’Reilly & Associates’ Learning Python. In the process, I stumbled upon two elements of Python that I thought were rather nifty: the string % operator, and the format method. The operator was the earlier of the two features, and is essentially an implementation of printf that takes the format pattern on the left of the operator and a list of values on the right, and puts them in according to printf-style format specs in the pattern.

But the format method, which came later, it’s a much different beast. It can do quite a bit more than the operator. Place-holders in the pattern can be named keys pointing into a dictionary (hash), or numbered indices into an array. And the range of formatting that can be applied to each interpolated value is quite a bit more extensive than what printf offers. Now, while I realize that the main impetus for Python’s string-formatting is that they don’t do in-string interpolation of variables the way Perl does, I still saw this as a possibly useful micro-templating approach. Imagine if the example above had looked more like this:

my $pattern =
    'This is a string with one {key1} followed by another {key2}.';
my $hash = { key1 => 'value', key2 => 'different value' };

my $string = $pattern % $hash;

OK, that’s still pretty contrived. And I’m not too good these days at making up examples that look like real-world problems. But consider this:

use URI;
use Const::Fast;

const $SPECBLOCK =>
    "Site: {name}\nProtocol: {url.scheme}\nPort: {url.port}\n" .
    "Host: {url.host}\n";

while (($name, $url) = splice @data, 0, 2) {
    my $block = $SPECBLOCK % { name => $name,
                               url  => URI->new($url) };
    print "$block\n";
}

That has a little more meat to it, yeah? And the use of an object as a value that can have methods called on it, that’s something that the Python format does, so surely a Perl copy should do so as well.

But there’s a catch: you can’t really do the above with Perl strings, because they aren’t objects derived from a class that you can monkey-patch. So I had to come up with a class, instead, to add the functionality to:

use URI;
use Const::Fast;
use YASF;

const $SPECBLOCK => YASF->new(
    "Site: {name}\nProtocol: {url.scheme}\nPort: {url.port}\n" .
    "Host: {url.host}\n"
);

while (($name, $url) = splice @data, 0, 2) {
    my $block = $SPECBLOCK % { name => $name,
                               url  => URI->new($url) };
    print "$block\n";
}

That will work. But wait, there’s more! And at no extra charge!

Because the format patterns are now objects, I can do things with them, to them, and on them. And the first thing I did, was introduce the concept of binding a data structure to the object to use as a default source if you had places where explicitly providing a binding wasn’t feasible. This opened up the ability to overload more operators, most notably the stringification operator:

use URI;
use Const::Fast;
use YASF;

const $SPECBLOCK => YASF->new(
    "Site: {name}\nProtocol: {url.scheme}\nPort: {url.port}\n" .
    "Host: {url.host}\n"
);

my %binding;
$SPECBLOCK->bind(\%binding);

while (($name, $url) = splice @data, 0, 2) {
    @binding{qw(name url)} = ($name, URI->new($url));
    print "$SPECBLOCK\n";
}

Again, even with more meat that is still a fairly contrived example, and a line or two longer than the previous version. But consider the following snippet:

use DBI;
use YASF;

# Set up database connection, then declare $sth as a fetch
# statement

# Declare $str as a YASF object with the pattern you want, with
# each field from the fetch statement available as a value in the
# pattern.

my %row;
$str->bind(\%row);
$sth->execute;
$sth->bind_columns( \( @row{ @{$sth->{NAME_lc} } } ));
while ($sth->fetch) {
    print "$str\n";
}

I based that off of the example in the DBI manual page of binding database columns directly to values inside a hash. I haven’t tested it yet, but I plan to add some tests to the distro soon, runnable when the user installing the module has DBD::SQLite available.

Anyway, there’s a lot more to the module, even though it is currently only at version 0.004. If this seems interesting, feel free to check it out on CPAN and/or GitHub. There are a few current caveats:

  1. Version 0.004 does not have any of the Python-ish formatting support, it only does expression substitution. If you put a format in as with Python, it will be quietly ignored for now. I am working on formatting right now, and hope to have a version 0.005 out before long.
  2. I still consider this alpha-quality software, and as such I may yet change some elements of the interface. In particular…
  3. …I’m debating whether to take the separate bind and binding methods and roll them into a single method. On the one hand, “bind” is more of a verb and thus a little more meaningful for the action of assigning bindings. On the other hand, most users (I suspect) would expect the same method that makes the bindings to return the current bindings. So this may change, especially once (if) other people start using this and give me some feedback.
  4. Lastly, I’m not too fond of the name “YASF”. I’m crap at naming things, and wrestled with this one for well over a month before a friend suggested “Yet Another String Formatter”. I would definitely entertain better suggestions, with the limitation that they need to be meaningful and (relatively) short. A long name with 2 or 3 parts to the namespace belies the general simplicity that I am aiming for.

I hope that someone (besides me) finds this useful.

(Afterthought: It feels good to write in this blog again. I do hope I can continue to do so in 2017…)

Tags: , , , ,