Reputation: 6578
I am using a script that traverses a HoH:
for my $bp (sort { $a <=> $b } keys $vars{$chr1}){
for my $chr2 (sort keys $vars{$chr1}{$bp}){
[...]
}
}
On my local computer (running perl v5.18.2
) this is accepted syntax. However, I am concerned about the portability of this section, as when I run this on my cluster (running perl v5.24.0
), the script dies with:
Experimental keys on scalar is now forbidden at script/mergesvs.pl line 66.
As I understand it, I can fix this by changing the above to:
for my $bp (sort { $a <=> $b } keys %{ $vars{$chr1} }){
for my $chr2 (sort keys %{ $vars{$chr1}{$bp} }){
[...]
}
}
But as I use the former syntax more often, I would prefer to use a version of perl compatible with this (e.g. v5.18.2
) in this script.
I've tried adding use 5.18.2
at the top of the script, but it makes no difference. Where should I specify what version of perl to use?
Or should I adopt the newer style in all of my code?
Upvotes: 2
Views: 173
Reputation: 118665
You can specify a minimum version with use
and a maximum version with no
.
use v5.14.0; # die if $] < 5.014 # When C<< keys EXPR >> was introduced
no v5.24.0; # die if $] >= 5.024 # When C<< keys EXPR >> was removed
no if $] >= 5.020, warnings => qw( experimental::autoderef );
but this is a terrible idea ... what Quentin said ...
Upvotes: 2
Reputation: 944202
You can test for the version of perl with $]
and do something like:
use v5.18.2;
if ($] >= 5.024) {
die("You are using a version of Perl too new for this program");
}
… but this is a terrible idea. You should be future proofing your code, not designing it to blow up if Perl is too new.
As the error message says, it was an experimental feature. It turned out to have lots of different problems.
You should stop using it and go back to using explicit dereferencing (which is not the new syntax, it is the original syntax after the new syntax turned out to be a bad idea).
Upvotes: 7