<?xml version="1.0" encoding="windows-1252"?>
<node id="294379" title="Re: Re: reference to an array slice?" created="2003-09-26 07:37:50" updated="2005-06-29 10:13:19">
<type id="11">
note</type>
<author id="171588">
BrowserUk</author>
<data>
<field name="doctext">
&lt;p&gt;It's a tad more efficient to use perl's built-in aliasing mechanism instead of a tie, at the slight cost of using an array reference rather than an array direct.

&lt;code&gt;
P:\test&gt;test3
1 2 3 4 It changed 6 So did this 8 9 10

       Rate   tie   sub
tie   473/s    --  -96%
sub 11689/s 2370%    --

0 0 0 -15402 -15402 -15402 -15402 -15402 0 0
&lt;/code&gt;

&lt;readmore&gt;&lt;code&gt;
#! perl -slw
use strict;
package Tie::Array::Slice;

use Tie::Array;

our @ISA = 'Tie::StdArray';

use Data::Dumper;
use strict;

sub FETCH { ${ $_[0]-&gt;[ $_[1] ] } }
sub STORE {
	ref $_[2]
		? $_[0]-&gt;[ $_[1] ] = $_[2]
		: ${ $_[0]-&gt;[ $_[1] ] } = $_[2];
}

package main;
use Benchmark qw[ cmpthese ];

sub aliasSlice{ return \@_ }

my @a = ( 1 .. 10 );

tie my @b, 'Tie::Array::Slice';
@b = ( \( @a ) )[ 3 .. 7 ];

my $c = aliasSlice( @a[ 3 .. 7 ] );

$b[ 1 ] = 'It changed';
$c-&gt;[ 3 ] = 'So did this';
print "@a\n";

our @aliased = (0) x 10;

cmpthese( -1, {
	tie	=&gt; q[
		tie my @alias, 'Tie::Array::Slice';
		@alias = ( \( @aliased ) )[ 3 .. 7 ];
		$_++ for @alias;
	],
	sub =&gt; q[
		my $alias = aliasSlice( @aliased[ 3 .. 7 ] );
		$_-- for @$alias;
	],
});

print "\n@aliased";

__END__
P:\test&gt;test3
1 2 3 4 It changed 6 So did this 8 9 10

       Rate   tie   sub
tie   473/s    --  -96%
sub 11689/s 2370%    --

0 0 0 -15402 -15402 -15402 -15402 -15402 0 0
&lt;/code&gt;&lt;/readmore&gt;
&lt;hr /&gt;
&lt;font size=1 &gt;
&lt;div&gt;Examine what is said, not who speaks.&lt;/div&gt;
"Efficiency is intelligent laziness." -David Dunham&lt;br /&gt;
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller&lt;br /&gt;
If I understand your problem, I can solve it! Of course, the same can be said for you.&lt;br /&gt;
&lt;/p&gt;&lt;/font&gt;
</field>
<field name="root_node">
294263</field>
<field name="parent_node">
294371</field>
</data>
</node>
