2020/07/03

Some facts about `use VERSION` in Perl

As it has been getting a lot of mention lately, I thought I'd spend a few minutes to remind/inform everyone about some facts about perl's use VERSION syntax. Some of this was even new to me either today, or at least a few days ago, so I fully expect this to be informative to at least a few people and clear up some misconceptions.

  • The ability to write a version in "v-string" notation has been part of core perl since at least 5.6. [March 2000, according to Wikipedia].

    $ perl5.6.2 -e 'use v5.8;'
    Perl v5.8.0 required--this is only v5.6.2, stopped at -e line 1.
    BEGIN failed--compilation aborted at -e line 1.

    At one time these v-strings felt new and strange, and the advice was not to use them for fear of back-compat issues. At this point in time, unless you need backward compatibility to something older than perl 5.6, you have nothing to fear by using a v-string.

    I literally didn't know this until today. So far I have code on CPAN that defensively does use 5.026 but now I know this I will be changing it all to use v5.26.
  • From perl version 5.12 onwards [November 2012], the use VERSION syntax also implies use strict. Thus, if you don't need to maintain backwards compatibility to any perls older than 5.12, you can replace

    use strict;
    with
    use v5.12;

    Not only is that one character shorter to type, it also brings in the say feature. I don't know about anyone else, but I find that extremely useful in small test/debugging situations.

    v5.12 also brings in the state, switch and unicode_strings features. The latter of these does alter the behaviour of regexp patterns and other string functions with respect to unicode characters, so if you are planning to turn this on I would recommend reading perldoc feature and perldoc perlunicode to find out more about that before doing so. Quoted here in brief:

    Safest if you "use feature 'unicode_strings'"

    In order to preserve backward compatibility, Perl does not turn on full internal Unicode support unless the pragma "use feature 'unicode_strings'" is specified. (This is automatically selected if you "use 5.012" or higher.) Failure to do this can trigger unexpected surprises. See "The "Unicode Bug"" below.

  • In October 2019 an issue was raised requesting that the next use VERSION, which would be v5.34, also enable the "warnings" pragma, much as 5.12 enabled "strict":

    Issue #17162

    This was debated for a while, with the current prevailing opinion being to not do this because enabling "use warnings" just because someone requested "use v5.32" was considered too radical and surprising:

    comment by @iabyn

    comment by wagnerc

No comments:

Post a Comment