Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Why Perl Docs examples do not use strict and warnings often

by Anonymous Monk
on Nov 20, 2021 at 12:17 UTC ( #11138973=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I was browsing through the Perl Data Structures documentation and noticed that though the document suggests using strict, it's not used in any examples.

How's someone who's new to Perl supposed to know when some internally provided variable is being used vs when a user created variable is being used in the code?

  • Comment on Why Perl Docs examples do not use strict and warnings often

Replies are listed 'Best First'.
Re: Why Perl Docs examples do not use strict and warnings often (updated)
by haukex (Bishop) on Nov 20, 2021 at 12:31 UTC
    I was browsing through the Perl Data Structures documentation and noticed that though the document suggests using strict, it's not used in any examples.

    Actually, it says "WHY YOU SHOULD ALWAYS use strict".

    And what alternative do you suggest? Putting it in every example would make the examples longer and the document harder to read, which is why there's a whole separate section near the start of the document explaining it. Many of the examples also run fine in the absence of strict and warnings. <update> To clarify, obviously one should still always use strict and warnings. But the documentation does still cater to those who don't. Plus, not every code snippet is intended to be a full program. If a code example were to fail in the presence of use strict; use warnings;, I'd call that a documentation issue. But again, I still don't see any concrete suggestions in this thread. </update>

    perlintro, the Camel, Modern Perl, and many other resources on learning Perl explain fairly early on why one should always Use strict and warnings. use v5.12; and up, which is required for many modern features to work, also turns on strict.

    How's someone who's new to Perl supposed to know when some internally provided variable is being used vs when a user created variable is being used in the code?

    From context and from trying out the examples.

    So just in case this isn't a troll: Feel free to submit patches for cases that you think are unclear.

      I love Perl and have zero interest in trolling my beloved language.
        I love Perl and have zero interest in trolling my beloved language.

        Anonymous posts with complaints and no suggestions to improve things tend to give that impression, unfortunately.

Re: Why Perl Docs examples do not use strict and warnings often
by LanX (Sage) on Nov 20, 2021 at 13:39 UTC
    Hi

    yes you are right, and I noticed it too. It's annoying, especially when teaching Perl.

    The reason is - like so often with Perl - legacy.

    There are also other cases like bareword filehandles.

    I suppose some docs have already been written in Perl4 times.

    IMHO perldsc and some other perldocs need to be proofread and updated.

    I'm glad that perlintro is pretty clean, though.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: Why Perl Docs examples do not use strict and warnings often
by Anonymous Monk on Nov 20, 2021 at 13:54 UTC
    Just to make it clear, I have no intention of trolling Perl. Though I've used it infrequently, the language has helped me a lot. It's the only language I use, and since I'm not a developer, I don't have time to learn another language. Perl does everything I need. Just needed to bring up what I observed. My foremost apologies if my question appears as if I'm trolling, but that is most certainly not my intention.

      For years, I claimed I was not a developer. For the same years, I used primarily Perl for the same reason you do.

      Your observation is valid and appreciated. I too find it conflicting when I see statements like "Always do X and Y", but then the examples don't follow suit. I suppose some of us take for granted the things we've ingrained our neural pathways with.

      One of the fortunate, yet unfortunate long-standing design aspects of Perl was and has been to keep backward compatibility at the forefront of importance. For this reason, strict isn't enabled by default. It's up to us to remember to do so. When a newcomer comes along and doesn't see that in all examples, I can understand it being something easy to forget. To people like me who have been coding in Perl for a couple of decades, it's like remembering to put butter on my toast.

      I think that all examples in Perl documentation may be better served by having the strict (and possibly warnings) lines, if every document doesn't make that clear in the intro explicitly. Unfortunately, I have not the time to make those changes to all documentation, so I'm not going to say it "should" be done.

      The best we can do as a compromise as far as I see, is continue informing people the importance of such mechanisms, and reminding them why they exist (and in some cases, why they aren't set by default). It's new coders like you who notice these important conflicts and acknowledge them that allows us all to renew conversation on the matter, so other new coders can see, understand and learn.

      Safe programming can be done without use strict; and use warnings;, but there's not a single file of Perl code I've ever written, deployed or published that don't have those two lines at the top.

      Good question.

      -stevieb

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11138973]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2022-05-19 03:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?