|Perl Monk, Perl Meditation|
What I Most Recently Learned in Perl, But Should Have Already Knownby liverpole (Monsignor)
|on Aug 16, 2006 at 22:43 UTC||Need Help??|
When I began learning Perl, almost exactly 6 years ago, it was so much fun that I couldn't get enough of learning new things. I had bought Learning Perl, vol. 2, a couple of years earlier, but hadn't made much of a dent in it, mostly because I wasn't involved in actually using it for any programming. So just reading about it was doomed to failure, in retrospect.
But in the summer of 2000, I was using it for work, and loving the process. The book "Learning Perl" (and several other newly-acquired Perl books as well) now became not only useful, but a source of new learning and fresh inspiration each time they were opened. It even became a habit to read chapters from Perl books in my spare time, just to learn all the cool constructs I had never encountered in any other languages. I still remember the excitement I felt discovering for the first time about tied variables. Or grasping the beauty of data structures which behaved one way in scalar context, and another way in list context, and the sheer beauty and logic of such a system. Or the intricate wealth of reusability that came from writing object-oriented modules.
And of course, the plethora of functions! Every time I researched this still-new (to me) language, there were all the functions that I loved in C, and new ones everywhere I looked. Add to that the complex user-defined data structures, sophisticated regular expressions, elaborate subroutine closures ... "Swiss-army Chainsaw" didn't even begin to describe the power!
But I have to admit I don't spend the same long hours reading about Perl the way I used to. I'm sure it's a common phenomenon for many; you look up what you need, when you need it, and that's about all. I did recently purchase Mark Jason Dominus' book, "Higher Order Perl", and although it's a fascinating and thoroughly illustrative book, I confess that I only read maybe 10-20 minutes of it at a time, not the 2-3 hours that I used to spend entrenching myself in learning. One gets to a point, perhaps, where it one feels that they know essentially all of the basics; no more big surprises when it comes to the fundamentals.
So it was today that I surprisingly came across something simple that I realized I didn't know how to do. While writing a program to demonstrate the application of the Bailey-Borwein-Plouffe formula, I couldn't figure out how to to convert a very long string, containing a binary value, into a hexadecimal value. I knew about hex and oct, but there isn't a bin equivalent, and though sprintf will output in binary, there wasn't an obvious way to make Perl treat my string as binary on input.
When I came across the answer, I realized I had missed something basic, back when I was just starting out with Perl. The oct function can work with a variety of input bases (not just octal), by specifying the base at the front:
And the oct documentation even suggests a method of handling any of the common 4 bases, with:
So now I'm wondering what other basics I may have missed along the way, either because I never noticed them the first time through, or just didn't need to know them until now.
And I'm curious, and ask the question of you, my brethren: "What basic function, construct, or concept in Perl did you recently learn, that you were surprised to find you hadn't already known?"