Announcing cpan.el

The Cat's Eye Nebula, one of the first planetary nebulae discovered, also has one of the most complex forms known to this kind of nebula. Eleven rings, or shells, of gas make up the Cat's Eye. Credit: NASA, ESA, HEIC, and The Hubble Heritage Team (STScI/AURA) Acknowledgment: R. Corradi (Isaac Newton Group of Telescopes, Spain) and Z. Tsvetanov (NASA) The Hubble Space Telescope is a project of international cooperation between NASA and the European Space Agency. NASA's Goddard Space Flight Center manages the telescope. The Space Telescope Science Institute conducts Hubble science operations. Goddard is responsible for HST project management, including mission and science operations, servicing missions, and all associated development activities.

The CPAN shell is just another shell, so why not drive it from Emacs?

If you write Perl code in Emacs, you may have wondered why we don’t have a simple mode for driving the CPAN shell (at least I couldn’t find one!).

Well, I finally stopped wondering. It wasn’t that hard to rip out the sh-specific parts of shell.el and make a new mode for the CPAN shell.

Here’s the code:

https://github.com/rmloveland/cpan-el

It’s easy to load up and drive from Emacs:

(add-to-list 'load-path (expand-file-name "/path/to/cpan-el/"))
(setq cpan-file-name "cpan")

(require 'cpan)

To run it, type M-x cpan.

There aren’t too any bells and whistles yet (completion, etc.), but you it’s pretty small so feel free to hack away.

(Image courtesy NASA Goddard Photo and Video under Creative Commons License.)

Scripting JIRA

black-origami-dragon

JIRA is everybody’s favorite enterprise issue management system. It’s the system many of us just love to hate.

Unlike some vocal people on the interwebs, I don’t “hate” JIRA, but I like to keep it at arm’s length. My job is not to be a JIRA jockey (although I do know me some JQL, about which more below) — my job is to get shit done.

And getting shit done quickly, for me at least, is usually a function of being able to control my tools from the command line, including via scripts.

That’s why I have written several scripts for interacting with JIRA from the command line (they’re explained below). You can get them from Github.

Note that they use some hard-coded values that match the JIRA server where I work, which you’ll have to change. However they do at least use your .netrc for passwords, etc., so you should be able to change them for your own use with a quick sed one-liner.

The functionality isn’t there to fully replace your web browser, especially if you work at a company that insists on enterprise-level JIRA jockeying, with crazy themes and labels and stuff, but here’s what’s included as of this writing — for each command, I’ve marked whether it is “plumbing” or “porcelain”:

  • jira-get-issue (plumbing): View an issue’s JSON, which you can pipe through tools such as jq to build other more generic tools you can control from your text editor, scripts, etc.
  • jira-create-issue (porcelain): This one is “end-user-ready”, in the sense that you call it and it pops up your text editor of choice so you can write the issue description, and when you close your editor the issue is created for you!
  • jira-search-issues (plumbing): Uses JQL to search your JIRA, and returns a bunch of issues as JSON for you to fiddle with however you prefer.
  • jira-add-comment (porcelain): Like jira-create-issue, this one pops open your text editor to add a comment to an issue.
  • jira-get-issue-status (plumbing): Gets back JSON describing an issue’s status. Used to check the status to transition an issue forwards or backwards using jira-set-issue-status (below).
  • jira-set-issue-status (porcelain-ish): Depending on an issue’s status, bump it forwards or backwards to an adjacent status. Help output describes the statuses and their ordering. (I’m not so sure about this one; more design work is needed, it’s still not really “porcelain”.)

As an example of using one of the plumbing tools to make a more user-friendly CLI tool, I have one I call jli that just gives me a list of my currently open issues:

#!/usr/bin/env sh

jira-search-issues "assignee = rloveland AND status not in \ 
(Closed, Resolved)" | jq '.key + " " + .fields.summary' | sed -e 's/\"//g';

Since it’s an arbitrary JQL query returning JSON, it’s easy to imagine how you might extend this to give separate lists per-project, ordered by status, etc. You can make your own little terminal- or text-file-based dashboard, send yourself a daily digest email using a larger script, and so forth.

There are some others, but I only have them on my work computer. For example, I have one that checks JIRA for new tickets by diffing the list of my assigned tickets and my local TODO list, which is in a text file.

Obviously these tools could use further development to be more modular and general-purpose, and they don’t come anywhere near covering the whole JIRA API, but they’re pretty small and easy to modify for your own use.

Most importantly, I use them every day at my job, so I know they work and are useful.

Hopefully they are useful to you too!

(Image courtesy Terry Robinson via Flickr under a Creative Commons license.)

Emacs Compilation Mode Regexp for Perl 6

Emacs Compilation Mode Regexp for Perl 6

Stick this in your .emacs if you want Perl 6 support in Emacs’ compilation mode:

  (add-to-list 'compilation-error-regexp-alist 'perl6-stack-trace)
  (add-to-list 'compilation-error-regexp-alist-alist
               '(perl6-stack-trace .
                                   ("at \\([A-Za-z-_]+\\(\.p[l6m]\\)?\\):\\([[:digit:]]+\\)"
                                    1 3)))

I don’t know if this would be a good addition to perl6-mode, or if compilation mode regexps are supposed to live elsewhere.

If you like this sort of thing, see also: flycheck-perl6.

How to install Perlbrew on Fedora 20 and use it with (m)ksh

yen-bill-crane

This post details how to go about installing perlbrew on a Fedora 20 Linux system for a user whose default shell is in the ksh family, specifically mksh, which is the shell I use.

The instructions in each section can be used separately if you are just a Fedora user or just a (m)ksh user.

Installing Perlbrew on Fedora 20

If you try to install Perlbrew using the instructions on its website, Fedora barfs. Fortunately, the workaround you need is detailed in this Github issue. (Hint: read the whole thread and then do what wumpus says.)

Using Perlbrew with ksh or mksh

The perlbrew bashrc file uses a few “bashisms” (unnecessarily, I think, but it is a “bash” rc after all). I managed to hack them out and, instead of sourcing perlbrew’s default bashrc file, I now source a kshrc built by applying the patch below.

It’s working well for me so far under mksh, but feel free to leave a comment if it doesn’t work for you and I’ll try to help.

Happy Perlbrewing!

--- bashrc	2014-12-30 12:49:14.110446784 -0500
+++ kshrc	2014-12-30 13:15:58.750454519 -0500
@@ -1,6 +1,5 @@
 export PERLBREW_BASHRC_VERSION=0.72
 
-
 __perlbrew_reinit() {
     if [[ ! -d "$PERLBREW_HOME" ]]; then
         mkdir -p "$PERLBREW_HOME"
@@ -13,7 +12,9 @@
 }
 
 __perlbrew_purify () {
-    local path patharray outsep
+    path=
+    patharray= 
+    outsep=
     if [[ -n "$BASH_VERSION" ]]; then
         IFS=: read -ra patharray <<< "$1"
     fi
@@ -30,13 +31,13 @@
 }
 
 __perlbrew_set_path () {
-    export MANPATH=$PERLBREW_MANPATH${PERLBREW_MANPATH:+:}$(__perlbrew_purify "$(manpath)")
-    export PATH=${PERLBREW_PATH:-$PERLBREW_ROOT/bin}:$(__perlbrew_purify "$PATH")
+    export MANPATH=$PERLBREW_MANPATH:$MANPATH
+    export PATH=${PERLBREW_PATH:-$PERLBREW_ROOT/bin}:$PATH
     hash -r
 }
 
 __perlbrew_set_env() {
-    local code
+    code=
     code="$($perlbrew_command env $@)" || return $?
     eval "$code"
 }
@@ -64,8 +65,8 @@
 }
 
 perlbrew () {
-    local exit_status
-    local short_option
+    exit_status=
+    short_option=
     export SHELL
 
     if [[ $1 == -* ]]; then

(Image courtesy Japanexperterna.se under Creative Commons License.)

Announcing confluence2html

../img/kyoto-swan.jpg

If you use (or used to use) a Confluence wiki, you may need to deliver content that was written in wiki markup to HTML. Confluence does have the ability to export an entire space to HTML, but not a single page (or section of a page). To overcome this limitation, I’ve written a script called confluence2html which can convert a subset of Confluence’s wiki markup to HTML. (You can check it out at my Github page – For examples of the supported subset of wiki syntax, see the t/files directory).

Unfortunately, the new versions of Confluence use a hacky “not-quite-XML” storage format that is terrible for writers and for which there are basically no existing XML tools either. If you are trying to get your content out of a newer version of Confluence and back into wiki markup, check out Graham Hannington’s site (search the page for “wikifier”). His page also has tools to help you wrangle the new format, if you care to (I don’t).

With a bit of editing, you should be able to get the output of Graham’s tool to conform to the subset of Confluence wiki markup supported by this script. It supports some common macros (including tables!), and I’ve found it really useful.

Right now, it’s a command line tool only. You can use it like so:

$ confluence2html < wiki-input.txt > html-output.html

If you don’t know how to run commands from the shell, you can read about it at LinuxCommand.org. If you are on Windows, you can run shell commands using Cygwin. If you’re on a Mac, open Utilities > Terminal in Finder.

At this point I’ll quit blathering and point you to the Github page. Again: for examples of the supported subset of wiki syntax, see the t/files directory. For documentation on the supported wiki syntax, see the README.

Happy writing!

(Image courtesy of caribb under a Creative Commons license.)

How to calculate the circumference of a circle (without knowing pi)

Some time ago I posted the following little challenge to www.reddit.com/r/programming:

Dear Proggit: Imagine that you must devise a method to calculate the circumference of a circle of any radius. The catch? You are unaware of the existence of Pi.

The other catch? You must begin right now. You may use only what tools you have in front of you (text editor, calculator, pencil and paper with compass(!)), without looking anything up. That means no Google searches, no Wikipedia, no formulas in a book, nothing.

Once you have devised a method, please write a small program in the language of your choice that performs the calculation (the more accurate, the better). Bonus points will be awarded for brevity, cleverness, etc.

I will post my little solution sometime tomorrow. Enjoy!

One final note: If you are a total geometry/math wizard who thinks this is lame, please refrain from posting; I want this to be a fun problem for those of us who will get something out of it.

https://logicgrimoire.files.wordpress.com/2012/09/wpid-circle-diagram.png

Below is my method for calculating (or rather, estimating) the circumference of a circle without using pi. This method involves summing the lengths of the hypotenuses of smaller and smaller triangles, as shown in the diagram above.

Begin by cutting the given circle into quarters. Using the radius r, find the hypotenuse of triangle A using Pythagoras’ method. Cut the hypotenuse in half; using that, and the radius, you can calculate the length of line segment Q. Subtract Q from r, and you have the length of line segment P, which is one side of the right triangle B. That, combined with half the hypotenuse of A, will allow you to calculate the hypotenuse of B. Continue, calculating the hypotenuses of smaller and smaller triangles. Sum as many lengths as are required to cover the quarter-circle arc, and multiply by 4 (or multiply the current length by 2^(n*+1), where *n is the number of iterations of the method you’ve performed).

Below I’ve provided an implementation in Perl. Note that the Perl interpreter doesn’t optimize away recursive function calls (that I know of), so you could in theory blow up the stack. In practice, it only takes a few iterations to arrive at a reasonable estimate, so it’s not a problem.

#!/usr/bin/env perl

use strict;
use warnings;
use autodie;
use Math::Trig ':pi';

my ($radius, $maxiter) = @ARGV;
my $iterations = 0;

sub main {
  my ($radius, $a, $b) = @_;
  my $hyp = hypotenuse($a, $b);
  my $newbase = $radius - sqrt($radius**2 - ($hyp / 2)**2);
  $iterations++;
  die "Current hypotenuse is $hyp after $iterations iterations\n"
    . "Estimated circumference: " . $hyp * (2**($iterations+1)) . "\n"
      . "Actual circumference: " . ($radius*2) * pi . "\n"
        if $iterations == $maxiter;
  main($radius, $newbase, ($hyp / 2));
}

sub hypotenuse {
  my ($a, $b) = @_;
  return sqrt($a**2 + $b**2);
}

main($radius, $radius, $radius);

As you can see, we pretty much mirror the textual description given above, but in code. We then compare our work to the “real” value of pi using the Perl core’s Math::Trig constant. Save the above into a file named circ.pl, and you can run it like so:

mel@foo:~$ ./circ.pl 4 12 # Given a radius of 4, run through 12 iterations
Current hypotenuse is 0.00306796150057116 after 12 iterations
Estimated circumference: 25.132740612679
Actual circumference: 25.1327412287183

I might not want to try to calculate planetary orbits using this kind of rough estimation, but for 99% of real-world cases, it’s good enough.