Tuesday, February 24, 2015

Perl property method?

package Foo

sub new

# bar property
sub bar
    my ($self, $text) = @_;
    if ($text)
        $self->{bar} = $text;
        return $self->{bar};


my $foo = Foo->new;

$foo->bar; # returns value of $self->bar
$foo->bar("baz"); # sets $self->bar = "baz"

Friday, May 9, 2014

HEREDOC for Windows Batch Files

From Wikipedia: "In computer science, a here document (here-document, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file."

Here's one way to accomplish this in a Windows batch file. Inside the batch file, echo the text to an output redirection, e.g. "> here.txt". Use carets "^" followed by double linebreaks to output on separate lines:


echo This is my ^

multi-line here document ^

that this batch file ^

will print! > here.txt

When executed here_text.bat will create a new file here.txt containing the lines of text:

This is my
multi-line here document
that this batch file
will print!

Monday, March 3, 2014


Noticed this rather defensive SSID in passing from my bus commute. I'm not sure if it's the WiFi or the Wife that's under guard, but the security's pretty robust. 

Thursday, May 16, 2013

Do-it-yourself uptime command for Windows 7

How long has my computer been running? A fundamental question that Unix-y systems can answer with the uptime command.

For some odd reason, Windows 7 has no built-in uptime command. You can simulate this by displaying the date and time Windows 7 last rebooted as reported by the net statistics command. Put this in a file named uptime.bat somewhere on your path:

@echo off
for /f "tokens=1,2,*" %%i in ('net statistics workstations ^| find "since"') do @echo %COMPUTERNAME% up since %%k

Then open a command prompt and type in the freshly minted command:

MYCOMPUTER up since 5/15/2013 5:21:27 PM

Saturday, May 11, 2013

Ruby, Ruby

You enter the j'Edit Five bistro from the steamy Paris evening. A sultry blonde, encased in a tight red dress, leans into the light over the toolbar at the far end of the room. She beckons to you with a heavy-lidded gaze, one eye obscured behind a curtain of flowing golden locks.

You step toward her, confident in that instant you're about to begin a beautiful friendship. You carry your plans and UML diagrams tucked inside the breast pocket of your jacket, a sheaf of ideas and ambition that only she can satisfy.

But as you draw near, you see the faint crow's feet around her eyes, the smudges on her dress, notice the style is seasons out of date. For the first time, you see the stringy, diminutive man in striped coveralls and a bill cap perched on the stool at her far side. A large red stone twinkles from the ring he twists around his pinkie.

You feel the heat from her body as you slide onto the bar stool beside her. "What is your name," you ask.

"Ruby," she breathes, "Ruby One Eight Seven." You inhale the cloying musk of her perfume, notice the slight trembling in her slim fingers as she lifts a tall flute of champagne to crimson lips. A trickle of moisture slides down the groove by her chin, spotting the front of her dress.

You try to mask your disappointment. You had so anticipated this meeting, a chance to make glorious music. But she is old, her skin creasing as she fits on a smile. She is deprecated, you think.

"And your little friend?" you ask, returning the railroader's nod.

"Him? He is only Rails Two Oh Three," her lip curls as she sneers. "He is just hoping to get lucky again tonight, but we are finished."

"You were so good together," you say, "I saw you performing together here years ago, up on the Web Two Oh stage."

"Yes," she says, "but I am tired. I live only to remember how I once danced here. The man who introduced us to j'Edit, he has gone away. And my younger sister, Ruby Two, has taken the limelight. Mais, she cannot dance here, my plugin is too old for her. So she prefers to dance for the Vim on Rue Rivoli, the Cafe Sublime, and le Palais de RubyMine." As she sweeps a cascade of gold away from her furrowed brow, the glitter of tears in her eyes reflects the flash of the red gems on her delicate earlobes.

You turn your gaze to the dim room. It is a good room, the windows edged with chrome, cups of Java steaming on the tables. Many coders and hackers huddle in booths by the glow of their screens, pecking out lines of Perl and Python, HTML and Scala. An aging XML roue nudges a tangled mass of angle brackets through the XSLT parser, chuckling as his XPaths return nodes.

"It could happen again," you say, "I could update the plugin to bring Ruby Two and her companion Rails Four back into the j'Edit. We could see them light up the windows again. She could parse, and colourize, and even debug in this room again. What do you say? Shall we find your little sister, and bring her back here?"

She tips her head back, stilling the quiver in her lip. "I will take you to the source, where we will set out the latest plugin for her, and repair the compiler errors and deprecations. We will update the mode to fit her. I may never dance here again, but my little Ruby Two, she will, and you will see to it. It will be her day, Ruby Two's day."

She demurs when you offer your hand. Instead, she lifts her satin clutch from the dark wood of the bar, and strides on her tall heels toward the door, a new vigor in her step. She pauses, and glances back at you. "Well, shall we?" she asks, not waiting for your answer as she exits, letting in the rush of traffic on the warm air.

Sunday, April 14, 2013

Cygwin bash: inspect the contents of .gitignore files

When git complained
The following path is ignored by one of your .gitignore files:puppet/modules/postgresql
it neglected to tell me which file. I used bash find to search all of the .gitignore files in my project:
$ find . -name .gitignore -print -exec cat '{}' \;
The "-print" option produces the file path, then the "-exec" option calls the "cat" command to dump the contents of the file, passing the file path to cat in the '{}' argument.

Saturday, April 13, 2013

Vagrant: how to fix "VM inaccessible" error

I've hit an annoying error a couple of times when trying to "vagrant up" a box after waking Windows 7 laptop from hibernation:
Your VM has become "inaccessible." Unfortunately, this is a critical error with VirtualBox that Vagrant can not cleanly recover from. Please open VirtualBox and clear out your inaccessible virtual machines or find a way to fix them.
This message came from vagrant 1.1.5. Under the hood, vagrant calls the command-line VBoxManage utility provided by VirtualBox to manipulate the VMs. vagrant helpfully traps the error flag produced by VBoxManage and serves up that vague yet soothing error message. Although considerate, the message doesn't really help me diagnose the problem. Fortunately, when run in debug mode (set VAGRANT_LOG=debug then run vagrant status), vagrant will cough up all the nasty error messages from VBoxManage.

After some digging through the debug output, I discovered that even though the actual VM is intact (I can load and run it from the VirtualBox GUI app), somewhere in its guts, VirtualBox flagged this VM as "<inaccessible>". Vagrant, rightly believing what it's told, spits out the error message.

After looking at VBoxManage's help, I found that one its commands, list vms, unsurprisingly lists all of the VMs registered with VirtualBox:

$ /cygdrive/c/Program\ Files/Oracle/VirtualBox/VBoxManage.exe list vms
"precise64" {3613de48-6295-4a91-81fd-36e936beda4b}
"<inaccessible>" {2568227e-e73d-4056-978e-9ae8596493d9}
"<inaccessible>" {0fb42965-61cb-4388-89c4-de572d4ea7fc}
"<inaccessible>" {c65b1456-5771-4617-a6fb-869dffebeddd}
"<inaccessible>" {

One of those VMs flagged as inaccessible is my lost VM! Time to fix VBoxManage's wagon, by unregistering the VM as inaccessible, then re-registering it with the correct name:
  1. Open the configuration file for your lost VM. Mine was saved to C:\cygwin\home\Philip\VirtualBox VMs\rails-vm-v2\rails-vm-v2.vbox
  2. Find and copy the value of the uuid attribute of the Machine node. Mine was 9709d3d5-ce4d-42b9-ad5e-07726823fd02.
  3. In a Windows command prompt (or Cygwin terminal), unregister the VM with the unregistervm command, using the [uuid] value from step 2:
    $ C:\Program Files\Oracle\VirtualBox\VBoxManage.exe unregistervm [uuid]
  4. Now register the VM using the registervm command, with the path to the VM configuration file:
    $ C:\Program Files\Oracle\VirtualBox\VBoxManage.exe registervm C:\cygwin\home\Philip\Virtual VMs\rails-vm-v2\rails-vm-v2.vbox
Now you should be able to start the VM as expected.