#!/usr/bin/perl -w
use strict;
{
package FixedArrayOfScalarRefs;
use Carp qw( croak );
sub TIEARRAY {
my ( $class, @refs ) = @_;
my $self = [@refs];
bless $self, $class;
return $self;
}
sub FETCH {
my ( $self, $index ) = @_;
if ( not ref $self->[$index] ) {
croak "Array index $index out-of-bounds";
}
my $value = ${ $self->[$index] };
return $value;
}
sub STORE {
my ( $self, $index, $value ) = @_;
if ( not ref $self->[$index] ) {
croak "Array index $index out-of-bounds";
}
${ $self->[$index] } = $value;
}
sub FETCHSIZE {
my ($self) = @_;
return scalar @$self;
}
}
####
my ( $a1z, $a2z, $a3z ) = qw( 42 13 666 );
my @az;
tie @az, 'FixedArrayOfScalarRefs',
"There is no element zero!", # Dummy element 0
\( $a1z, $a2z, $a3z ); # Elements 1 .. 3
for ( 1 .. 3 ) {
print "\$a${_}z = $az[$_]\n";
}
####
print "\$a0z = $az[0]\n";
# Array index 0 out-of-bounds at bar.pl line 63
####
use List::Util qw( sum );
my $sum = sum( @az[ 1 .. 3 ] );
print "sum = $sum\n";
####
my $sum = sum( @az );