|Welcome to the Monastery|
Don’t Repeat Your… version numberby Aristotle (Chancellor)
|on Jul 20, 2006 at 23:01 UTC||Need Help??|
Now, that in itself is a damn cool hack.
But it immediately set my mind thinking about how to use it for the one thing that always annoys me about module maintenance: updating the module version in both your POD and on the $VERSION line.
Turns out, this is actually very tricky because you have to get the polyglot understood by three different tools:
In other words, something like this, which was my first thought, won’t work:
If you try that, you will find that parse_version will eval just this:
I had to resort to treachery: reading MakeMaker’s source to find its weaknesses. And the weakness, it turns out, is that it uses /^=cut/ to stop skipping. Notice something? That matches too many things… Gotcha! You’re going down.
Unfortunately, the single-line requirement means that the version number must be on the same line as the string $VERSION, which means we’ll have to have that in the POD:
Here, the heredoc operator on the first line sets perl up to treat the entire following section as a string. In that string we look for a line with an equals operator, then eval it.
The =for line makes POD formatters ignore what’s on the next line, unless one of them thinks it’s the formatter for the output format called “fooling”, for which a formatter is unlikely to ever be written.
And what’s on the next line, the =cut-feigned, makes parse_version stop skipping and look for a line which sets $VERSION.
Would I use this is actual CPAN-published code? I don’t know. But you have to admit, it is really quite a fun hack.
Makeshifts last the longest.