http://www.perlmonks.org?node_id=638020

There have been some posts lately, inspired by Project Euler's problems. I also play with PE, although on a sparse basis and having joined much late.

In a particular problem it turned out to be of much use Perl's ability to return undef when accessing an array element beyond its actual length, and the promotion of undef to zero in a numeric addition. Except that it triggers the well know warning, in which case the usual cure is to locally disable it. (Since the actual script body was 14 lines, I eventually committed for once the sin of commenting out the use warnings; line altogether.)

Of course I may have used a (IMHO) visually obtrusive ||0 instead...

The point is, in this case the intended usage is more in line with common mathematical one, especially as in combinatorics. As an example consider the following minimal implementation of a program to print the first ten rows of Pascal's triangle:

#!/usr/bin/perl -l use strict; # use warnings; my @row; while ($row[1]<10) { @row=(1, map $row[$_-1]+$row[$_], 1..@row); print "@row"; } __END__

If I enable warnings I'll get two for the comparison for the first two iterations and one for the addition per each row.

So I wonder if one day a predefined attribute may be specified on an array so that accessing it beyond its limits would return 0 rather than undef. Perhaps something like

my @row :zero;

Also, Perl's current behaviour with negative indices is occasionally practical but also not best suited for some mathematical-like usages. So still in the brainstorming vein as above, perhaps another attribute should exist, say :nocirc, to the effect of returning undef (or 0 if in conjunction with :zero) when accessing negative indices. Thus the code above may become the clearer - for the mathematically inclined:

my @row :zero :nocirc; while ($row[1]<10) { @row=map $row[$_-1]+$row[$_], 0..@row; print "@row"; }

Admittedly, this is not Perl's everyday job. Still it may be useful. I would add this to ysth's 5.12 wishlist.