Hello and welcome to Rick Umali's Tech Talk. I'm a computer professional (IT developer) living and working in Greater Boston.
Feel free to send me e-mail (rickumali@gmail.com) or comment on any item. Thanks for visiting!
I spend a lot of time in the DOS Command Window. And I spend a lot of time being curious about my Windows PATH. Examining the directories in your PATH can be a demoralizing exercise, but a Perl one-liner makes reading it very manageable.
Here's the output of PATH on my work laptop:Quick: how many directories are there? That's right...unless you can quickly count the semi-colons, it's hard. It's even harder to look at the PATHs to pick out the individual directories.
Now here's the one-liner, which you can type directly into your DOS window (provided Perl is in your PATH):
When I enter this on my laptop, I get this clean, legible output:
So how does this work? The key to this one-liner is Perl's 0 (zero) switch. The -0 tells Perl what it should use for its input record separator (this is an AWK concept). 0x3B is the hexadecimal value of the ";" (semi-colon). When -0x3B is used with the -n and -l switches (-n for "automatic looping", and -l for "automatic line-ending processing"), it basically says "loop through this string, treating every string that ends with a semi-colon as a single record." The print command passed to the -e switch (-e for "run the following argument as a one line program") auto-increments the line number.
Coming up with one-liners in Perl rely on understanding Perl's rich command line switches, two of which (-n and -p) cause a while loop to be created out of thin air, and another two (-a with -F and -0) cause the input "records" to be manipulated before being passed to a program (via -e).
To run this program on UNIX, you'll have to replace the input record separator with colon (0x3A):