1s44c has asked for the wisdom of the Perl Monks concerning the following question:

Perl Monks,

I'm confused about what use version actually does. According to 'perldoc -f each' In Perl 5.12 and later only, it will also return the index and value for the next element of an array so that you can iterate over it; older Perls consider this a syntax error. On a system using perl 5.10.1:

#!/usr/bin/perl -w use v5.10.1; my @array = qw/one two three/; while (my ( $index, $line ) = each @array) { print "$index: $line\n"; } Result: Type of arg 1 to each must be hash (not private array) at ./t8 line 7, + near "@array) " Execution of ./t8 aborted due to compilation errors.

That's exactly as expected. What I don't get is why does that same code execute on Perl 5.18.2? It works like I never specified 'use v5.10.1'. Shouldn't 'use version' disable syntax that doesn't work on the given version?

Replies are listed 'Best First'.
Re: use version confusion
by AppleFritter (Vicar) on Jul 06, 2014 at 12:29 UTC

    To the best of my knowledge, use v5.10.1 merely indicates to perl that you'd like to enable all features that exist in v5.10.1 and that your script is not expected to run on older perl versions, or conform to their restrictions and limitations. It cannot be used to "downgrade" a newer perl version to an older one by emulating the old version's behavior.

    It is possible to use $^V (aka $PERL_VERSION if you use English;) to check the version of perl you're running on and use different code for different versions, but this is of debatable utility. If you need your code to run on older perl versions, it's best to just stick to constructs they allowed and not use newer features.

      Is there any way to downgrade a recent Perl to an older one? Or do I just have to be really careful not to use features that don't exist on older versions?

        Test your code at least on the minimum and maximum version of Perl you want to support (and if you want to be thorough, the ones in between too).

        You can easily have different Perl versions installed on your system. See for example perlbrew.

        If by "downgrade" you mean by some kind of option to make a newer Perl behave like an older one, then no, at least not completely: While you can disable features, that won't disable all of the things that the newer Perl supports. Only actually using the older Perl version will give you reliable test results.

        None that I'm aware of (though I'm hardly the most experienced monk in the Monastery, so maybe one of the senior brothers will enlighten me). You'll just have to be careful.
        Just look for deprecation warnings. Perl is excellent at backward compatibility, but if a feature is marked "experimental" or deprecation warnings start appearing, then you might want to pay good attention and refactor your code that relies on these.

        You can also always, test against blead.

        PerlBrew.
Re: use version confusion
by Anonymous Monk on Jul 06, 2014 at 13:06 UTC

    use VERSION; defines the minimum Perl version that the script will run on (see use). In addition, it implicitly loads the feature bundle associated with that version. (Note: no VERSION; will limit the maximum Perl version, but that usage is probably pretty rare. You can also use no to disable feature bundles.)

    The each docs, towards the end, say this:

    To avoid confusing would-be users of your code who are running earlier versions of Perl with mysterious syntax errors, put this sort of thing at the top of your file to signal that your code will work only on Perls of a recent vintage:

    use 5.012; # so keys/values/each work on arrays use 5.014; # so keys/values/each work on scalars (experimental) use 5.018; # so each assigns to $_ in a lone while test

    So your script is not quite right in declaring use v5.10.1;.

Re: use version confusion
by Anonymous Monk on Jul 06, 2014 at 12:41 UTC
    As far as I know, use VERSION enables features and strictures. Use 5.010 will only use features available in 5.10, so it can be used to 'downgrade' Perl in that sense. But each @array' is not a 'feature'. See 'perldoc feature'.