|Perl Monk, Perl Meditation|
When do you include the usual use pragmas in your example code?by rjt (Deacon)
|on Aug 03, 2013 at 12:59 UTC||Need Help??|
tl;dr? See title.
It seems like we monks spend a significant amount of time admonishing Seekers of Perl Wisdom to use strict;, use warnings;, and the like. But when posting example code in response to someone's question, what do you include? I've recently begun to question my own habit of almost always including strict (or more recently, use VERSION), warnings, and even the oft-unnecessary shebang. I don't for one-liners or when interleaving multiple fragments among paragraph text.
If recent answers are any indication, equally experienced monks seem to favor both sides of the coin (including a few fans of Modern::Perl). I'm starting to think, however, that where these near-universal statements are concerned, less is more. Why?
First, there are already myriad places strongly urging strict and warnings, and people still submit questions with fundamental bugs which would have been caught had they heeded that advice. In those cases, I'm very likely to specifically mention strict/warnings and show the OP how those pragmas help solve their specific problem. However, in other cases, I doubt quietly including it at the top of my code that answers, say, how to refer the index of an array to another array of the same length is likely to cause an epiphany.
Second, two or three lines are sufficient to answer many questions. But those lines often get lost in the noise of the pragma parade, which significantly bloats the post. While conciseness isn't everything, see above paragraph, and ponder: do those extra lines help answer this poster's question? I believe most of the time, the answer is "no".
But then there is say. Without use 5.010; (or later), use feature 'say';, or perl -E, someone running my example code sees only a moderately unhelpful syntax error:
I use say() rather quite a lot, less so with some of the other new features. At what point do we assume (most) people get the picture and at least know how to turn the features on? A loaded question, I admit. Perhaps I'm jaded by the scores of "do my homework" posts, or perhaps posting answers without the necessary pragma is just begging additional confusion.
Finally, we're not a code writing service. I don't wish to make it overly difficult to run my examples, but my first responsibility is to improving the OP's understanding of Perl (and anyone else who's curious, I suppose). I'm not quite sure if that's an argument for or against.
All of this thinking has led me here: At the moment, I'm leaning toward dropping the boilerplate unless it seems directly relevant to the question, but updating my signature with a one-liner something like this:
What do the rest of you think? Perhaps I'm putting too much thought into this, but, then again, maybe putting this question to bed once and for all will silence that irritating voice in my head that starts muttering every time I wrap a two line example in four lines of boilerplate. :-)