Its been a while

I haven’t been posting much lately. Lets see what I’ve bee up to:

  • I’ve repaired 6 Chromebooks this week. There will be more tomorrow.
  • Apple is replacing a damaged iPad because I am wicked polite and prepared with documentation.
  • I’m not going up the 20′ ladder. Just no.
  • I’ve finished yet another stocking, except for the name. I’m putting it off because I’m not sure about placement. Probably, I should make it easy to remove and change if they want to.
  • Accidentally ruined my favorite hiking boots.
  • Took some pictures.
  • Knit a hat.
  • Bought Christmas cards. I’ll start filling them out as soon as I’m done with the stocking.

All in all, life has been pretty good!

Perl: Spaces in a function or method name

I accidentally stumbled over an interesting ability/quirk in Perl: a subroutine / function / method name may contain spaces. Since I couldn’t find any info about it in the perlsub man page or on Google I decided to write it down.

It should be obvious that you can’t create such a subroutine by defining it the traditional way, but in case it isn’t: you can’t. Perl will consider the first word to be your subroutine identifier, and the following word(s) to be invalid keywords.

use strict;

sub name With Spaces
{
    print "works!\n"; # liar, doesn't work
}
Illegal declaration of subroutine main::name line 4.

NOTE: the following examples were tested in Perl versions 5.8.8 (circa 2006), 5.14.2 (circa 2011), and 5.28.2 (circa 2019).

To create a method name with a space, you have to manipulate the symbol table directly. (Indeed, I figured it out by accident thanks to an AUTOLOADed method that did that.)

sub AUTOLOAD
{
    my $self = shift;

    ( my $method = $AUTOLOAD ) =~ s{.*::}{};

    if ( exists $self->{_attr}->{ $method } ) {
        my $accessor = sub { return shift->{_attr}->{ $method } };

        {
            no strict 'refs';
            *$AUTOLOAD = $accessor;
        }

        unshift @_ => $self;
        goto &$AUTOLOAD;
    }

    return;
}

Stated more simply:

my $name = "name With Space";
*$name = sub { "works!" }; # insert directly to symbol table

Utilities like Test::Deep “just work” if there’s a space:

cmp_methods( $obj,
             [ 'name With Space' => 'value' ], # not a peep!
             'Basic methods'
            );
ok 1 - Basic Methods

The obvious question, though, is how to access it directly?

You can access a method using a variable, which is a pretty common thing to do on it’s own. (In my experience, anyway, YMMV).

my $name = 'name With Space';
my $value = $obj->$name; # works!

You can also create a reference to a string and immediately deference it.

my $value = $obj->${ \'name With Space' }; # works!

The second example works with regular function calls as well. Here’s a stand-alone example:

use strict;

{
    no strict "refs";
    my $name = "name With Space";
    *$name = sub { "works!" };
}

print ${ \"name With Space" }, "\n";' # prints "works!"

I can’t recommend creating subroutines with spaces in the name as good style, but it’s helpful to know that it can happen and how to work with it when it does.

Highlight of the Day

Meghan and I just heard an MBTA commuter train sound out “shave-and-a-haircut” on it’s airhorn.

New LEAF

Meghan got a new job here in town a while back.  More to the point, it’s two miles away.

I realized after a while that her car wasn’t really getting a chance to get up to operating temperature very often, even in the middle of summer.  She makes several trips throughout the day to various buildings around town, but they’re rarely more than a couple of miles per hop.

This is the worst-case scenario for a gasoline-powered car.  Her mileage suffered considerably: she was barely getting 20 mpg.  I knew that the car would shortly show signs of fast aging — this is part of the “severe driving” section of the manual.

After hemming and hawing a bit, we decided that the best way to protect our asset would be to go electric.  (I can’t say “protect our investment” because, lets face it, cars are not investments.)  Meghan is the best-case scenario for an electric: lots of starts and stops (which lets regenerative braking recapture lots of energy) and no range anxiety since she’s never far from home.

We did our research, tried out a few cars, and finally found a good match.

Introducing Meghan’s new-to-her 2016 Nissan LEAF SL

Meghan's New LEAF
Meghan in her new Nissan LEAF.

Another shot of the front:

Front of LEAF

CSC 350

You can do this in a number of ways. IBM chose to do all of them. Why do you find that funny?

— D. Taylor, Computer Science 350

Favorite New Word of the Day

Crapulence

noun
The feeling of sickness or indisposition resulting from excess in eating or drinking

Highlight of the Week

My personal highlight of this week:

Just finished a 20 mile bike ride with Meghan.  We’re sitting in a McDonalds in Danvers.  At the table next to us, we’re listening to a 99 year old guy (gonna be an even 100 next month!) come over and start chatting up a group of 80-something women.

New Word of the Day

Satisfactorian

noun
a person who’s responsibility includes delivering maximum satisfaction to their clientele