Always better to wrap in a subroutine.
use Data::Dump qw(dump);
my @dna = qw(A T T G C);
print +(dump \@dna),$/;
my @view;
make_view( \@dna, 1, \@view, 0 );
make_view( \@dna, 2, \@view, 1 );
#mutate
$view[0] = 'x';
$view[1] = 'x';
print +(dump \@dna), $/;
make_view( \@dna, 4, \@view, 0 );
print +(dump \@dna), $/;
# print ["A", "T", "x", "G", "x"]
sub make_view {
my ($arr, $idx1, $view, $idx2) = @_;
tie $view[$idx2], 'Overridden', sub : lvalue { $arr->[$idx1] };
return;
}
{
package Overridden;
sub TIESCALAR {
my ($class, $item) = @_;
return bless \$item, $class;
}
sub FETCH {
my $self = shift;
${$self}->();
}
sub STORE {
my $self = shift;
my ($val) = @_;
${$self}->() = $val;
}
}
But, that's what I would do.
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.