Help passing data to subroutine

Help passing data to subroutine

am 01.12.2009 23:34:23 von Barry Brevik

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@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help passing data to subroutine

am 01.12.2009 23:38:34 von Michael Ellery

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 @data = (1, 2, 3);
foo(\@data);

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

...or did I misunderstand you?

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help passing data to subroutine

am 02.12.2009 00:14:16 von Serguei Trouchelle

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 @arr = eval '@' . $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 @arr1 = qw/a b c/;
my @arr2 = qw/d e f/;

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

ch_r(\@arr1);

print_arr('arr1');

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

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

--
Serguei Trouchelle
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help passing data to subroutine

am 02.12.2009 01:28:04 von Williamawalters

--===============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@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 @array1 = qw(A B C);
print_ra('array1');
print_ra('array2');
our @array2 = qw(D E F);
print_ra('array2');
{ my @array2 = qw(x y z);
print_ra('array2');
}
sub print_ra {
my $array_name = shift;
no strict 'refs';
print qq{print_ra: (@{ $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

hi barry --=
  



In a message dated 12/1/2009 5:34:34 PM Eastern Standard Time, BBrevik=
@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=20

'hard' reference.   as you will see if you look through your=
hard copy=20

references and through perlref and perlreftut on-line, symbolic refere=
nces=20

are generally considered a Bad Thing for reasons that have been touche=
d=20

upon in other responses.   the use of hard references is con=
sidered a best=20

practice for the best of reasons.   



however, there a time and place for everything.   take a loo=
k at the code=20

below; if you understand and feel comfortable with it (and think your=
=20

maintainers will as well), go ahead and use symbolic references.  =
; but=20

heed well the many warnings against such things!   



>perl -wMstrict -le

"our @array1 =3D qw(A B C);

print_ra('array1');

print_ra('array2');

our @array2 =3D qw(D E F);

print_ra('array2');

{ my @array2 =3D qw(x y z);

  print_ra('array2');

  }

sub print_ra {

  my $array_name =3D shift;

  no strict 'refs';

  print qq{print_ra: (@{ $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--

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

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0046238523==--