Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • This certainly is covered in Damian Conway's PBP []:

    "5.1. Lexical Variables
    Avoid using non-lexical variables."

    And preferred use of my (or local for certain built-ins) is repeated in a dozen or more specific contexts like filehandeles, builtin vars, ....

    # I had a sig when sigs were cool
    use Sig;
    • This certainly is covered in Damian Conway's PBP: "5.1. Lexical Variables Avoid using non-lexical variables."

      I'm not referring to lexical variables (my), as you pointed out that part is covered by the best practices. I was referring to writing a single script from the top to the bottom with out localizing all variables. Remember that my only declares the variable for the life time of the current block. If the variable is not declared in a block then the file is the block scope, making the variable global. This is no matter if the variable is written in all caps or in lower case

      I think that we have learned that declaring all variables as global is a bad programming practice. We are lucky with Perl that we don't need to do so. We can even declare our variables just before we need to. Other languages, C for instance, are not that lucky and are trying to address this in some way or another (C99 is doing it, a bit too late).

      I like wrapping all my code inside a function, it limits the scope of the variables and doesn't allow me to cut corners. If I need something global it has to be pulled out manually from the block. Moving bits of code from the main function to another function is easier because if I forget a variable use strict; will spot it right away. Without this refactoring code that omits a variable will pass unseen as all variables are global.

      • hear hear!

        Whenever I encounter a substantial (>100 lines) perl script with no main sub I get nervous.

        A quick, one-off script may not require the extra structure and discipline, but we all know how easily a one-off can become mission-critical... and quickly grow in size scope and function.

        In my opinion, file-scoped vars are nearly as bad as true globals, and packaged-scoped vars less-so, but still best to avoid, unless the alternative is sufficiently more complicated code somewhere else.

        By using a main bl