In Perl, why is the $[ variable in Perl v5.16.0 a problem?

Viewed 93

From the O'Reilly book "Learning Perl", p38:

'* Array and list indices always start at zero in Perl unlike in some other languages. In early Perl, it was possible to change the starting number of array and list indexing (not for just one array or list but for all of them at once). Larry later realised that this was a misfeature, and its (ab)use is now strongly discouraged. But, if you're terminally curious, look up the $[ variable in the perlvar manpage.

How exactly could this feature (which is now unexisting) be abused?

2 Answers

As implied by the spelling ("(ab)use"), it's mere use is abusive. Using $[ does nothing but add complexity, the thing programmers strive to eliminate. This is what lead to its removal.


In Lua, tables (which serve the purpose of arrays) use 1-based indexes, which led do this conversation just two days ago:

"Well that's unintuitive AF"

Now imagine if each Perl module could select its own value for $[... It's best we avoid this type of problem!

You can use it because, as of 5.16, it hadn't been removed. Using 5.24.0:

$ perl -e 'use strict; use warnings; $[ = 3'
Use of assignment to $[ is deprecated, and will be fatal in Perl 5.30 at -e line 1.

It's deprecated, but still there.

Related