Help passing data to subroutine

Using Active Perl 5.8.8.

I have a situation where I specifically want to pass the name of an
array to a subroutine, and allow that subroutine to modify values in the
caller's copy of the array.

I feel incredibly stupid, because I have a bunch of Perl books and have
worked on this a long time, and can not figure it out!

Ideally, the subroutine will never have a copy of the array in it's own
space, but will only operate on the array in the caller. Can anyone show
me how this is done?

Barry Brevik
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Barry Brevik [ Di, 01 Dezember 2009 23:34 ] [ ID #2024563 ]

Re: Help passing data to subroutine

Barry Brevik wrote:
> Using Active Perl 5.8.8.
>
> I have a situation where I specifically want to pass the name of an
> array to a subroutine, and allow that subroutine to modify values in the
> caller's copy of the array.
>
> I feel incredibly stupid, because I have a bunch of Perl books and have
> worked on this a long time, and can not figure it out!
>
> Ideally, the subroutine will never have a copy of the array in it's own
> space, but will only operate on the array in the caller. Can anyone show
> me how this is done?
>
> Barry Brevik


It sounds like you want to pass an array reference:

my [at] data = (1, 2, 3);
foo(\ [at] data);

sub foo
{
my $ref = shift;
$ref->[0] = 0;
# etc...
}

...or did I misunderstand you?

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Michael Ellery [ Di, 01 Dezember 2009 23:38 ] [ ID #2024564 ]

Re: Help passing data to subroutine

Barry Brevik wrote:

> I have a situation where I specifically want to pass the name of an
> array to a subroutine, and allow that subroutine to modify values in the
> caller's copy of the array.

my [at] arr = eval ' [at] ' . $name;

Though PBP says it's bad, because it's compiling during eval so it takes time, and also because it's harder to find
problems and even comprehend the code.

> Ideally, the subroutine will never have a copy of the array in it's own
> space, but will only operate on the array in the caller. Can anyone show
> me how this is done?

I'd use hash with name keys and arrayref values, like $array{$name} = [1, 2, 3];

Then you can pass \%array and $name to your subroutine.

About caller's copy: if you pass a reference to sub, it's always the same reference, and modifying some values will
modify "original" data.

A small example:

___CUT___
#!/usr/bin/perl -w

use strict;
use warnings;

my [at] arr1 = qw/a b c/;
my [at] arr2 = qw/d e f/;

print_arr('arr1');
print_arr('arr2');

ch_r(\ [at] arr1);

print_arr('arr1');

sub print_arr {
my ($name) = [at] _;
my [at] arr = eval " [at] " . $name;
print join(', ', [at] arr), $/;
}

sub ch_r {
my ($ref) = [at] _;
$ref->[1] = 'x';
}

--
Serguei Trouchelle
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Serguei Trouchelle [ Mi, 02 Dezember 2009 00:14 ] [ ID #2024659 ]

Re: Help passing data to subroutine

--===============0046238523==
Content-Type: multipart/alternative;
boundary="part1_bfa.52708ce6.38470e94_boundary"


--part1_bfa.52708ce6.38470e94_boundary
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit

hi barry --

In a message dated 12/1/2009 5:34:34 PM Eastern Standard Time,
BBrevik [at] StellarMicro.com writes:

> I have a situation where I specifically want to pass the name of an
> array to a subroutine, and allow that subroutine to modify values in
> the caller's copy of the array.

this is called a 'symbolic' or 'soft' reference, and is in contrast to a
'hard' reference. as you will see if you look through your hard copy
references and through perlref and perlreftut on-line, symbolic references
are generally considered a Bad Thing for reasons that have been touched
upon in other responses. the use of hard references is considered a best
practice for the best of reasons.

however, there a time and place for everything. take a look at the code
below; if you understand and feel comfortable with it (and think your
maintainers will as well), go ahead and use symbolic references. but
heed well the many warnings against such things!

>perl -wMstrict -le
"our [at] array1 = qw(A B C);
print_ra('array1');
print_ra('array2');
our [at] array2 = qw(D E F);
print_ra('array2');
{ my [at] array2 = qw(x y z);
print_ra('array2');
}
sub print_ra {
my $array_name = shift;
no strict 'refs';
print qq{print_ra: ( [at] { $array_name })};
}
"
print_ra: (A B C)
print_ra: ()
print_ra: (D E F)
print_ra: (D E F)

good luck, and choose wisely -- bill walters


--part1_bfa.52708ce6.38470e94_boundary
Content-Type: text/html; charset="US-ASCII"
Content-Transfer-Encoding: quoted-printable

<HTML><FONT FACE=3Darial,helvetica><FONT SIZE=3D2 PTSIZE=3D10>hi barry --=
  
<BR>
<BR>In a message dated 12/1/2009 5:34:34 PM Eastern Standard Time, BBrevik=
[at] StellarMicro.com writes:
<BR>
<BR>> I have a situation where I specifically want to pass the name of=
an
<BR>> array to a subroutine, and allow that subroutine to modify values=
in
<BR>> the caller's copy of the array.
<BR>
<BR>this is called a 'symbolic' or 'soft' reference, and is in contrast to=
a
<BR>'hard' reference.   as you will see if you look through your=
hard copy
<BR>references and through perlref and perlreftut on-line, symbolic refere=
nces
<BR>are generally considered a Bad Thing for reasons that have been touche=
d
<BR>upon in other responses.   the use of hard references is con=
sidered a best
<BR>practice for the best of reasons.   
<BR>
<BR>however, there a time and place for everything.   take a loo=
k at the code
<BR>below; if you understand and feel comfortable with it (and think your=

<BR>maintainers will as well), go ahead and use symbolic references.  =
; but
<BR>heed well the many warnings against such things!   
<BR>
<BR>>perl -wMstrict -le
<BR>"our [at] array1 =3D qw(A B C);
<BR> print_ra('array1');
<BR> print_ra('array2');
<BR> our [at] array2 =3D qw(D E F);
<BR> print_ra('array2');
<BR> { my [at] array2 =3D qw(x y z);
<BR>   print_ra('array2');
<BR>   }
<BR> sub print_ra {
<BR>   my $array_name =3D shift;
<BR>   no strict 'refs';
<BR>   print qq{print_ra: ( [at] { $array_name })};
<BR>   }
<BR>"
<BR>print_ra: (A B C)
<BR>print_ra: ()
<BR>print_ra: (D E F)
<BR>print_ra: (D E F)
<BR>
<BR>good luck, and choose wisely -- bill walters   
<BR></FONT></HTML>

--part1_bfa.52708ce6.38470e94_boundary--

--===============0046238523==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0046238523==--
Williamawalters [ Mi, 02 Dezember 2009 01:28 ] [ ID #2024660 ]
Perl » gmane.comp.lang.perl.active-perl » Help passing data to subroutine

Vorheriges Thema: PPM broken after installing ActivePerl 5.10.1 b1006
Nächstes Thema: Help with PerlApp