light weight references and arrays

X

xhoster

Arrays hold some kind of special light-weight reference to it's scalar
members, but of course the reference it holds are not held in full scalars,
as that would cause an infinite regress. Is there some way to manipulate
these light-weight references directly?


The code below makes a copy of all the data in $y:

$ perl -le 'my @x=1..10; my $y = "x"x1e7; $x[0]=$y'

This codes doesn't make a copy, but now I need to manually derefence
it when I want to retrieve it from @x:

$ perl -le 'my @x=1..10; my $y = "x"x1e7; $x[0]=\$y'

And this code just doens't work:

$ perl -le 'my @x=1..10; my $y = "x"x1e7; \$x[0]=\$y'
Can't modify single ref constructor in scalar assignment at -e line 1, at EOF

I think the above code makes it clear what I intended to do, change the SV
that a certain array slot is referencing to be a different SV. Is there a
fundamental reason that Perl cannot be made to this, or is it simply that
it hasn't been coded that way?

A similar question goes for concatenating arrays--is there a way to
accomplish something like push @x,@y but having the scalars contained in @y
be directly incorporated into the end of @x, rather than being copied into
new scalars that are on the end of @x? (unlike the original, I don't know
what syntax would be used to imply this operation)


Xho
 
U

Uri Guttman

x> I think the above code makes it clear what I intended to do, change
x> the SV that a certain array slot is referencing to be a different
x> SV. Is there a fundamental reason that Perl cannot be made to
x> this, or is it simply that it hasn't been coded that way?

i think local can do that as it will work on any element in an array or
hash. should be easy to test for your needs. perl6 can do that almost
surely as it has a bind := op.

x> A similar question goes for concatenating arrays--is there a way to
x> accomplish something like push @x,@y but having the scalars contained in @y
x> be directly incorporated into the end of @x, rather than being copied into
x> new scalars that are on the end of @x? (unlike the original, I don't know
x> what syntax would be used to imply this operation)

what you want is lazy evaluation which perl6 will also have. you can
push a whole array and only when you access that section it will it be
accessed and then via the original array (if it hasn't been modified,
copy on write rules are in effect).

of course i could be wrong this week as perl6 changes faster then a
submachine gun based tv remote control!

uri
 
D

Dr.Ruud

(e-mail address removed) schreef:
Arrays hold some kind of special light-weight reference to it's scalar
members, but of course the reference it holds are not held in full
scalars, as that would cause an infinite regress. Is there some way
to manipulate these light-weight references directly?


The code below makes a copy of all the data in $y:

$ perl -le 'my @x=1..10; my $y = "x"x1e7; $x[0]=$y'

This codes doesn't make a copy, but now I need to manually derefence
it when I want to retrieve it from @x:

$ perl -le 'my @x=1..10; my $y = "x"x1e7; $x[0]=\$y'

And this code just doens't work:

$ perl -le 'my @x=1..10; my $y = "x"x1e7; \$x[0]=\$y'
Can't modify single ref constructor in scalar assignment at -e line
1, at EOF

I think the above code makes it clear what I intended to do, change
the SV that a certain array slot is referencing to be a different SV.
Is there a fundamental reason that Perl cannot be made to this, or is
it simply that
it hasn't been coded that way?

A similar question goes for concatenating arrays--is there a way to
accomplish something like push @x,@y but having the scalars contained
in @y be directly incorporated into the end of @x, rather than being
copied into new scalars that are on the end of @x? (unlike the
original, I don't know what syntax would be used to imply this
operation)

I think you are looking for
http://search.cpan.org/search?module=Data::Alias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top