As I’ve begun to dig into PowerShell, I’m reminded again of a whole cluster of themes: why programming languages differ, how they can share a common foundation, and what influences our ability to use a mix of languages according to their strengths and our preferences.
In this example I used a combination of PowerShell and Python because each afforded convenient access to a familiar idiom. In the case of PowerShell, the idiom was a certain style of XML processing. Of course the idiom is available in Python too, but even as a regular Python user I need to stop and think which module provides it.
In the case of Python, the idiom was a certain use of the regular expression machinery. I wasn’t familiar with the PowerShell/.NET idiom, but I knew it in Python, so I used what I knew.
Of course any programming language can be used to accomplish any programming task. You can write a game of Tetris in XSLT, for example, and people have. But each language has its sweet spot. Once you get comfortable in that zone, you want to stay there. It takes effort to be open to other languages’ sweet spots, and to the possibility of combining them.
When I suggested that a pattern dictionary of idioms could help grease the skids, Kevin Reid pointed to RosettaCode.org. Very cool! This would be a great place to enumerate variations on, for example, the regular expression idiom that allows the dot (period) character to match newlines. I have probably learned and forgotten a half-dozen times that SingleLine is not to be confused with multiline, SingleLine is a perl regex term, it corresponds to dotall in java regex. If we could reliably find patterns like allows “.” to match newlines and map from them to implementations in any language, we’d be in a better position to use multiple languages according to their strengths and our preferences.
Note that although my original example was done using the standard C-based version of Python, I later switched to IronPython in order to use the .NET regular expression machinery. It’s true that there are some idioms to be mastered in order to use that machinery from different .NET languages. But there’s also a large foundation of idioms shared by all .NET languages. Because PowerShell and IronPython rest on that common foundation, I’ll think we’ll be able to discover, as both evolve, more about what is essentially Pythonic and essentially PowerShellish.