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

Most potent, grave, and reverend signiors,

Are there plans to add runtime array bounds checking some time, or perhaps someone has written a module? Such a thing would have gone a long way in helping me with nailing a problem with a 10x10 array, whereby Perl (5.16.3) happily extended rows for me when I forgot to reset the column counter.

I envisage something like:

pragma arraycheck @myarray[10][10];

to enforce under pain of death that @myarray is 10x10, and some way to enable it at runtime from the command line during development.

Ta muchly.

-- Dave

Replies are listed 'Best First'.
Re: Array bounds checking
by SuicideJunkie (Vicar) on Sep 09, 2014 at 19:20 UTC

    Arrays don't have a fixed size, but you could make an object that stores an array and enforces its size during updates. There may already be tied arrays and such available on CPAN.

    Wanting a fixed-size array in the first place smells a bit odd in itself to me. What is your use case?

      I seem to be forever coming up with odd use cases. This one is a puzzle-solver, involving a 10x10 numeric grid of 1's and 0's (Binary Brainsnack if you're familiar with it). In hindsight I probably would've chosen another data structure.

      -- Dave

        In hindsight I probably would've chosen another data structure.

        It's never too late to refactor your code.

Re: Array bounds checking
by Laurent_R (Canon) on Sep 09, 2014 at 21:20 UTC
    The fact that arrays can grow to fit your needs is just one of the really great things with dynamic languages in general and with Perl in particular: no need to keep track of the size and to malloc new memory chunks.

    Now, having said that, if you want to implement such a limitation, it is quite easy to do it: just don't access directly to your arrays, but do it through a small set of functions that check for bounds and return an exception when your program gets off-limits. So you could implement special versions of push, unshift to check bounds, as well as a set_val and a get_val mutator and accessor functions to access to the array elements.

    Although I am not a great fan of OO programming in general, this is one case where it really makes sense and I might very probably implement it as an object-oriented module to create and manage arrays through a bunch of methods. (Or, perhaps, I would be tempted to do it with some closures, this usually makes it possible to do the thing in probably twice less code compared to OO code, but that's a different question and a matter of purely personal inclination.)

    As mentioned by other monks before, a tied array might be a more transparent solution, but I haven't been really convinced so far by tied arrays and hashes.

Re: Array bounds checking
by LanX (Saint) on Sep 09, 2014 at 19:32 UTC
    If there is not already a module on cpan (which I doubt) you could implement it with Tie::Array .

    Overwriting EXTEND () looks like the best approach.

    Be warned about the performance loss involved when using tie.

    Try searching cpan for other solutions.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      Thanks; I'll take a look at it.

      -- Dave

Re: Array bounds checking
by GrandFather (Saint) on Sep 09, 2014 at 22:41 UTC

    It's already been hinted that you could solve this with an OO solution. Actually, I'd have started with the board implemented as an object just so I could hide the "under the hood" board representation details and manipulate it at a high level that makes it easy to separate the puzzle solving logic from the board manipulation code.

    Perl is the programming world's equivalent of English