tags:

views:

178

answers:

3

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;
use v5.10;
use v5.10.0;

All seem to work. perlcritic complains about all but the first two. (It's unfortunate that the v strings seem to have such flaws, since Perl 6 expects you to do use v6; for your Perl 6 scripts...)

So, what should we be doing to indicate that we want to use a particular version of perl?

+4  A: 

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.

Michael Carman
+4  A: 

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.

Adam Bellaire
v is optional when there are 3 or more parts specified
ysth
@ysth: Sure, but the lead-in paragraph talks about using `v` strings, and then drops the `v` in the example without mentioning that they are optional. It's valid code but it doesn't match the lead-in.
Adam Bellaire
+3  A: 

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.

brian d foy
Great insight, thanks.
Robert P