Categories
Programming

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.

Categories
Programming

Komodo IDE Headaches

I’m slowly coming around to the idea that an IDE might be useful for PHP/Symfony projects (still not convinced about other languages and frameworks) and I’m currently trying out ActiveState’s Komodo IDE 10 on Linux.

It looks great but it’s… buggy.  One day in and I’m already getting frustrated with it.

  • The preference file doesn’t appear to be saved until the application is closed, if the application crashes it’s not clear that your changes will be saved.  This might be a safety feature, but probably not, because…
  • At least some preferences don’t take effect until the application is closed.  Not the ones that you’re warned about like checking remote files for changes, but other ones like ‘Allow file contents to override Tab and Indentation settings’ (which itself is unreliable since at least 2011).
  • When changing preferences, there is more than one place to change: Edit / Preferences, Edit / Current File Preferences, and Project / Project Preferences (the last is not under the Edit menu).
  • The cursor blinks by default (which is super annoying when you’re moving the cursor around the screen) and there isn’t an explicit option to disable it.  You have to create a custom JavaScript script that executes at every file open.
  • The toolbar icons are heavily styled, making their use opaque and the tooltips mandatory reading.
  • It has already crashed while closing — which, per the above, I’m doing a lot.

It’s not all bad, there are some really nice features:

  • Vi keybindings, so things like ‘A’ to start appending to the current line, or ‘/’ to search the current file, are really nice to someone who uses vim every day.
  • I do appreciate the ability to script things
  • The syntax highlighting and coloring seems more reliable (i.e. harder to confuse) than average.
  • The installation to a local directory was painless, and an icon properly shows up in the applications menu (I use Mate).  The default installation dir is to your home directory instead of /usr/local (which is the right thing to do for trial software, imho).

I want to like this editor, I really do, but it’s just going downhill as I work with it more.  At $250 per license it’s hard to justify the expense to my boss unless I really like it.

Categories
Fortune

Two Hard Things

There are only two hard problems in Computer Science: cache invalidation, naming things, and off-by-one errors

Categories
Programming

Rate Yourself From 1-10

This… is genius.

In technical programming interviews a common (terrible) question that interviewers may ask is, “rate yourself from 1-10 on x”, where x=one or more programming languages.  I’ve been asked that myself, but I’ve never seen what 1-10 would actually correspond to until now.  It’s a very fuzzy measure and most everybody (from junior to senior) seems to rate themselves about a 7.

Without further ado:

  • 10 – Wrote the book on it (there must be a book)
  • 9 – Could have written the book, but didn’t.
  • 8 – Deep understanding of corner cases and esoteric features.
  • 7 – Understanding and (appropriate) usage of most lesser known features.
  • 6 – Can develop large programs and deploy new systems from scratch.
  • 5 – Can develop/deploy larger programs/systems using all basic (w/o book) and more esoteric features (some w/ book, some without)
  • 4 – Can develop/deploy medium programs/systems using all basic (w/o book) and a few esoteric features (w/ book). Understands enough about internals to do nontrivial troubleshooting.
  • 3 – Can utilize basic features without much help, manage a small installation competently.
  • 2 – can write hello world without looking at a book, kind of figure out how a system works, if necessary.
  • 1 – Can read programs, make small changes to existing programs, or make adjustments to already installed systems, w/book handy.
  • 0 – No experience.

Credit goes to /u/icydocking for providing the list on reddit.

Categories
Uncategorized

Hello World!

I got my first two programs to run! Now – to do the homework. I can so totally do this. Chart tracker, here I come!

Categories
Uncategorized

My Dream App

So, you may know that I’m a knitter. And I’m addicted to my phone, tablet, etc. These things are largely compatible.

As a knitter, I print many patterns. Patterns from Ravelry. Patterns from Knitty (did you see that the new issue is up? YAY!). I think my pattern printing is responsible for at least one tree. And the only thing I really need the printouts for are the charts. Those lovely, complex lace charts. Set up, transitioning, number of repeats, all that can be found online, anytime. The charts must be printed so I can track what row I’m on. I usually use a highlighter, but any writing instrument will do in a pinch. And if I forget my chart? Forget any knitting until I get home. ARG!

There are many fine apps out there as row counters. That’s not what I want. I want an app that will display my chart, with the rows I’ve already done, highlighted. Just like my lovely paper chart, but less likely to be left at home.

So, I have the Android SDK, Java 7, and no real idea what I’m doing. This is going to be great. 🙂