On Wednesday 16 Dec 2009 16:57:01 Bryan R Harris wrote:
> [stuff cut out]
> 
> >> For example, if I'm populating a complex variable @d with
> >> lots of pointers,
> >> hashes, arrays, etc. within, if I populate that within a
> >> subroutine, how do
> >> I get it back out conveniently without it making a whole
> >> nother copy of it
> >> outside?  If it's 500 MB, isn't that horribly inefficient?
> >> Plus, I have to
> >> keep track of it.
> >
> > You pass as a refernce as ni
> > called_sub(\...@d);
> > Now when you update, you are updating @d and not a copy.
> >
> >          If you have any questions and/or problems, please let me know.
> >          Thanks.
> >
> > Wags ;)
> > David R. Wagner
> 
> So let's say I pass a reference to an array:
> 
>    my @d = (1,2,3);
>    called_sub(\...@d);
> 
> ... but then in called_sub, accessing that gets a lot "noisier", right?
> 
>    sub called_sub {
>        my $d = shift;
>        push @{$d}, 2;  # I'd rather be able to use @var instead of @{$var}
>    }
> 
> Is there any way to make a new variable, @something, that is just another
> name for the array that was passed in by reference?  Since I'm building a
> complex data structure, having to include all those @{}'s can get annoying.

First of all, often just "@$d" instead of "@{$d}" will work too. This will 
make things a little less noisy. Otherwise, you can try using perltie 
(possibly not very recommended) to tie a regular variable into the reference. 
Alternatively, you can use this module:

http://search.cpan.org/dist/Lexical-Alias/

Or another option I can think of is to create an object to wrap the operations 
around the reference, and use methods on it:

<<<
my $d = MyArrayHandle->($d_param);

$d->push(@other_array);
>>>

TIMTOWTDY. And in Perl 6 one will be able to pass arrays as first-order 
parameters to the subroutine.

> 
> Also, if called_sub modifies that array that was passed in by reference,
> does it stay changed outside the subroutine?  Or do I have to return
> something from the subroutine and capture it on the outside?

It stays changed outside the subroutine:

<<<<<<<<<<<<
#!/usr/bin/perl

use strict;
use warnings;

sub change_array_ref
{
    my $a_ref = shift;

    $a_ref->[5] = 5_000;

    return;
}

my @array = (1 .. 10);

change_array_ref(\...@array);

print join(", ", @array), "\n";
>>>>>>>>>>>>

prints:

<<<<<<<<
1, 2, 3, 4, 5, 5000, 7, 8, 9, 10
>>>>>>>>

That's part of the point of references. If you're interested in having a 
temporary variable, you can use a shallow copy ("[ @{$array_ref} ]") or a deep 
copy - see perldoc Storable.

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
"Star Trek: We, the Living Dead" - http://shlom.in/st-wtld

Bzr is slower than Subversion in combination with Sourceforge. 
( By: http://dazjorz.com/ )

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to