Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

When do you include the usual use pragmas in your example code?

by rjt (Deacon)
on Aug 03, 2013 at 12:59 UTC ( #1047703=monkdiscuss: print w/ replies, xml ) 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:

String found where operator expected at -e line 1, near "say "Just ano +ther Perl hacker,"" (Do you need to predeclare say?) syntax error at -e line 1, near "say "Just another Perl hacker,""

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:


Always assume use 5.012; use warnings; at the top of my examples. See feature, strict and warnings

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. :-)

Comment on When do you include the usual use pragmas in your example code?
Select or Download Code
Re: When do you include the usual use pragmas in your example code?
by hippo (Curate) on Aug 03, 2013 at 13:55 UTC

    It's an interesting concern and should probably remain up to the whim of the individual poster, but I like your solution of shifting the boilerplate to your sig.

    FWIW, I don't use say because the very, very small benefit it gives is far outweighed (for me) by the portability constraints. I think it would be fine to use it if the OP uses it or if the OP's code implies a recent enough version where it won't give an error.

    I think it is always worth including strict and warnings in any sample code other than a one-liner. If every example of code a novice sees includes these pragmas then even the slowest student will eventually twig that having them in there must be considered A Good Thing. Doing so also has the added advantage that I expect to see it in my own code or third-party code on which I am working and any absence there will become even more noticeable as a result.

    All just my opinions, of course.

Re: When do you include the usual use pragmas in your example code?
by toolic (Chancellor) on Aug 03, 2013 at 14:48 UTC
    I think it is most helpful to post a complete code example which compiles under strict without errors and runs under warnings without warning messages. By explicitly posting those 2 pragmas, there is no doubt as to how the code I posted runs.

    Regarding the signature, some monks (like me) disable it because it is a distraction from the rest of the post, but we are probably in the minority.

Re: When do you include the usual use pragmas in your example code?
by davido (Archbishop) on Aug 03, 2013 at 16:45 UTC

    I used to show use strict; and use warnings; at the top of almost all of my example code. Then I read and thought a lot about MJD's "Twelve Views, #8: Why I Hate strict". From that article, there's this:

    People come into the Usenet comp.lang.perl.misc group with some question, which they demonstrate with a four-line example program, and other folks there jump on them: ``Why didn't you use strict?'' Well, because it's a four-line example program I concocted as an example in my Usenet article---duh!

    After digesting that article I kind of shifted my thinking a bit. Now, often I'll post only the code necessary to demonstrate my point. Usually it's stand-alone and runnable. Occasionally just a snippet. I will use lexical variables almost always, but may not explicitly demonstrate "strict" and "warnings" if they're not part of the solution, or part of the point I'm trying to make.

    If I do advocate for the use of strictures and warnings, I'll also try to point out which aspect of "strict" would have caught some problem in the user's code, or which warning might have been raised had warnings been enabled. I feel this is far more helpful than simply carping "You should use strict and warnings." Another quote from the article:

    Any time you hear anyone saying ``you should be using strict,'' you can be fairly sure they're not thinking about what they're saying, because strict has these three completely unrelated effects. A more thoughtful remark is ``you should be using strict 'vars''' (or whatever). As an exercise in preventing strict zombie-ism, ask yourself ``Which of the three strict effects do I find most useful? Which do I find least useful?''


    Dave

      Too few people have read that MJD piece, much less understood it. Seeing it again now just makes me sad about what Perl and, much more, its community have become.
Re: When do you include the usual use pragmas in your example code?
by BrowserUk (Pope) on Aug 03, 2013 at 18:45 UTC

    Insisting that every snippet posted is a complete, stand-alone, pedant-compliant work of dogma; is like insisting on a full salt-throwing ritual before demonstrating tying a nappy on a doll. Ie. Meaningless overkill.

    Perhaps the only thing sillier is worrying about it :)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: When do you include the usual use pragmas in your example code?
by Happy-the-monk (Monsignor) on Aug 03, 2013 at 19:45 UTC

    Easy one: Whenever it is a relevant part of the answer/example given.

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

Re: When do you include the usual use pragmas in your example code?
by Anonymous Monk on Aug 03, 2013 at 19:50 UTC
      I have 202 writeups (now 203) and I was not going to post about way of responding to posts any time soon.
Re: When do you include the usual use pragmas in your example code?
by moritz (Cardinal) on Aug 03, 2013 at 20:33 UTC

    When I post a complete, runnable piece of code, I add the usual boilerplate. When I correct only a few lines of the OP's code, I don't.

    I find it rather important to include the use 5.010; when I use say (which I do a lot, actually) in my examples, because the error message is often quite bad when you forget to include the pragma (often it complains that there is no method say, because perl parsed it as indirect method syntax).

    And newcomers already have enough to struggle with, I don't want to increase their cognitive load by using a convenience subroutine that then makes their live harder.

Re: When do you include the usual use pragmas in your example code?
by rjt (Deacon) on Aug 04, 2013 at 02:54 UTC

    Excellent replies. (Keep 'em coming!) I'll bundle up a few replies to keep the page size down:

    hippo: It's an interesting concern and should probably remain up to the whim of the individual poster,

    I agree completely. Once in a while, my individual whim benefits from a little collective influence such as this.

    toolic: Regarding the signature, some monks (like me) disable it because it is a distraction from the rest of the post, but we are probably in the minority.

    I hadn't considered that. It's probably a pretty good bet that any monk PM-savvy enough (and active enough to care) to use custom CSS to hide pmsig blocks already knows a thing or ten about strictures and warnings (i.e., not part of the target demographic). From a continuity standpoint, though, probably best, then, not to rely on a post making sense iff the signature is visible.

    davido on Dominus: If I do advocate for the use of strictures and warnings, I'll also try to point out which aspect of "strict" would have caught some problem in the user's code, or which warning might have been raised had warnings been enabled.

    I do like that much better than the blanket "use strict;" 2x4 some monks wield (and I've no doubt taken a swing or two myself with).

    BrowserUk: Insisting that every snippet posted is a complete, stand-alone, pedant-compliant work of dogma; is like insisting on a full salt-throwing ritual before demonstrating tying a nappy on a doll. Ie. Meaningless overkill.

    I think your analogy itself may have been meaningless overkill, but that won't get in the way of me agreeing with your overall sentiment. :-)

    BrowserUk: Perhaps the only thing sillier is worrying about it :)

    Perhaps. I could say I see it more as "optimizing" than "worrying", but then I could be rightly accused of worrying about semantics.

    moritz: And newcomers already have enough to struggle with, I don't want to increase their cognitive load by using a convenience subroutine that then makes their live harder.

    This ↑ ↑ ↑ is the whole point I was dancing around, I think: i.e., minimizing cognitive load. Well said.


    So, where does that leave me? There's still some room for me to be swayed, but what I take away from these replies (colored a bit by my own preferences of course) is that it makes sense to ditch the boilerplate unless it's relevant, but including any pragmas required to successfully run the code (i.e., use 5.010; in examples with say) is still worth it.

    Again, thank you all for the most thoughtful replies. Yes, even you, Anonymonk. Please keep them coming, that is, if there's anything more to say.

Re: When do you include the usual use pragmas in your example code?
by sundialsvc4 (Abbot) on Aug 05, 2013 at 20:27 UTC

    (Shrug ...)   I simply think that everything which you choose to place within your <code>...</code> tags should be “complete.”   The already-confused Gentle Reader™ ought to be able to copy-and-paste exactly what you gave him, run it, and wind up with exactly the outcome that you refer to.   Both the strict and the warnings pragmas have a very-distinct and therefore very-important influence upon whether-or-not this actually occurs, since Perl, by-default, will DWIAABITYM = Do What I (As A Binary Idiot) Think You Mean   ;-)   Both of them also, by default, are not included.   Therefore, by all means, include them.   (I am, already, confused-as-hell ... therefore, please don’t leave me guessing ...)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: monkdiscuss [id://1047703]
Approved by Corion
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2014-10-01 20:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (38 votes), past polls