What's the modern way of declaring which version of Perl to use?

后端 未结 3 557
猫巷女王i
猫巷女王i 2021-02-01 07:08

When it come to saying what version of Perl we need for our scripts, we\'ve got options, oh, brother, we\'ve got options:

use 5.010;
use 5.010_001;
use 5.10.0;
u         


        
相关标签:
3条回答
  • 2021-02-01 07:31

    The "modern" way is to use the forms starting with v. However, that may not necessarily be what you really want to do.

    Critic complains because older versions of Perl won't understand and play nicely with the forms that start with v. However, if your version of Perl supports it, v is nicer to read because you can say:

    use v5.10.1;
    

    ... rather than ...

    use 5.010_001;
    

    So, in the documentation for use, the following workaround is offered:

    use 5.006; use v5.6.1;
    

    NB: I think the documenation is in error here, as the v is omitted from the example at perldoc use.

    Since the versions of Perl that don't support the v syntax will fail at the first use, they won't get to the second more specific and readable one.

    0 讨论(0)
  • 2021-02-01 07:37

    There are really only two options: decimal numbers and v-strings. Which form to use depends in part on which versions of Perl you want to "support" with a meaningful error message instead of a syntax error. (The v-string syntax was added in Perl 5.6.) The accepted best practice -- which is what perlcritic enforces -- is to use decimal notation. You should specify the minimum version of Perl that's required for your script to behave properly. Normally that means declaring a dependency on language features added in a major release, such as using the say function added in 5.10. You should include the patch level if it's important for your script to behave properly. For example, some of my code specifies use 5.008001 because it depends on the fix for a bug that 5.8.0 had which was fixed in 5.8.1.

    0 讨论(0)
  • 2021-02-01 07:40

    I just use something like 5.010_001. I've grow weary of dealing with version string problems for something that should be mind-numbingly simple.

    Since I mostly deal with build systems, I have the constant struggle of Module::Build's internal version.pm which is out of sync with the version.pm on CPAN. I think that's mostly better now, but I have better things to think about.

    The best practice should always be to do the thing that commands the least of your attention, and certainly not take more attention than the value it gives back. In my opinion, v-strings and dotted decimals were a huge distraction with no additional benefit, wasting a lot of valuable programmer time just to get back to the starting point.

    I should also note that Perl::Critic has often pushed questionable practices for the higher purpose of reducing the ways that people do things. However, those practices often cause problems, make them un-best. This is one of those cases. A more realistic best practice is to not make Perl::Critic compliance your goal. Use it where it is useful, but in cases like this, don't waste mental time on it.

    0 讨论(0)
提交回复
热议问题