I have a confession to make.

I still use Perl. Regularly. But mostly as a command line tool. Is that a reason? or an excuse?

When I started using Python regularly, I tried to replace my Perl usage.
Nothing against Perl, I just wanted to force myself to use Python to aid in my learning of the language.

I still have quite a few Perl habits that are hard to break.

The trouble is that some things are just so darn easy in Perl (if you’ve already learned it), and not so easy (as far as I know) in Python.

Here’s an example.

When I start writing what I know is going to be a long blog post, I start with a bit of an introduction, followed by a table of contents. The TOC is really an outline of what I want to write. I want to write it first so that the flow of the post makes sense before I start writing the content.

When I’m happy with the TOC, I want to convert it into a bunch of h2 headers.

Since I’m using markdown, it looks a bit like this:

* [first section header](#anchor_to_first_section)
* [second section](#second)

What I want is to throw that code into something that will spit out:

<h2 id="anchor_to_first_section">first section header</h2>
<h2 id="second">second section</h2>

Perhaps there’s an easy way to do this with Python, but I don’t know it.
I usually use Perl, because that’s what I know how to use from the command line.

My Perl solution.

I select the TOC in my editor, and copy to the copy buffer, then paste it on the command line in an echo command and pipe it to a small Perl script, like so:

> echo '* [first section header](#anchor_to_first_section)
* [second section](#second)
'  | perl -pe 's/\* \[(.*)\]\(#(.*)\)/<h2 id="$2">$1<\/h2>/'
<h2 id="anchor_to_first_section">first section header</h2>
<h2 id="second">second section</h2>

I know it looks like gobbledygook.
But as I’m writing the regular expression, it makes perfect sense, and I don’t have to think about it much.

After running the command, I select the output, and copy/paste it into my editor.
Not really an efficient process, but better than doing all of the typing myself, especially for long TOCs.

My history with Perl and Python.

I started using Perl in probably 1992, I think (back when the camel book was small). I had to learn it for some university classes, used it for web cgi scripts, and then continued to use it professionally mostly as a if-its-too-complex-for-a-bash-ksh-script scripting language. I also learned to use it as a better-than-sed command line tool. I built a couple of in-house tools with Perl and tk (but… Yuck!).

I started using Python in probably 2001-ish on the job. The remote programming test scripts for the instruments I wrote code for were written in Python, with a custom in-house made test runner. My usage of Python has grown since, and it has supplanted many projects that I would have selected Perl, c++, or bash in the past. Especially if the tool needed any sort of GUI, as wxPython is reasonably painless to use.

However, Perl had almost a decade head start in my neurons, and I think some it may be hardwired now.
Perhaps it’s similar to why I use vim bindings and vim key mappings in sublime text. Really, it’s not me hitting half the keys anymore, it’s my fingers. And they learned vim a long, long time ago. (Right after I tried to write a plugin for Emacs, actually.)

Any suggestions?

I haven’t taken the time to learn to use Python as a command line tool.

Is something like the above example possible in Python?