Re: subroutines, prototyping, and pass by reference question
- From: "Eric" <emartin24@xxxxxxxxx>
- Date: 11 Jan 2006 10:13:38 -0800
Paul Lalli wrote:
> Eric wrote:
> > Perhaps I should have been more clear. I have tried working with
> > references and all of the documentation does great on showing me *how*
> > to do it. The reason for my post was more to figure out the *why* and
> > *when*. I provided a sample so that any comments would make more sense
> > in the context of something I have tried as opposed to an example in
> > the documentation.
>
> There are three main reasons to use references:
>
> 1) To avoid copying a large piece of data when passing into a
> subroutine:
>
> my $string = 'sometext' x 1_000_000;
> #we now have an 8 million character string;
> process_string($string);
> sub process_string {
> my $arg = shift;
> #we have now copied our 8 million character string
> # ... do stuff with the $arg;
> }
>
> Using references:
> my $string = 'sometext' x 1_000_000;
> #we now have an 8 million character string;
> process_string(\$string);
> sub process_string {
> my $arg_ref = shift;
> #we now have a reference to our one and only 8 million character
> string
> # ... do stuff with $$arg_ref;
> }
>
> The difference, of course, is that in the first example, any changes
> you make to $arg are independent of the original $string. In the
> second block, any changes you make to $$arg_ref will be reflected in
> $string when the subroutine ends.
>
> 2) Passing multiple arrays/hashes and keeping them distinct.
>
> my @foo = (1..5);
> my @bar = ('a' .. 'e');
> process_arrays(@foo, @bar);
> sub process_arrays {
> my @args = @_;
> #all ten values are now in @args. There is no way of knowing
> #where 'foo' stopped and 'bar' began in the original. We have one
> flat
> #array which contains 10 values
> }
>
> Using references:
> my @foo = (1..5);
> my @bar = ('a' .. 'e');
> process_arrays(\@foo, \@bar);
> sub process_arrays {
> my ($arr1, $arr2) = @_;
> #We now have the five 'foo' values in @$arr1, and the five 'bar'
> #values in @$arr2. We can use these two sets of values distinctly
> #from each other.
> }
>
> 3) Building multi-dimensional structures.
> There is no way to create, for example, a two-dimensional array without
> using references. Arrays hold scalars. Arrays cannot hold arrays. It
> simply doesn't work:
> my @not_two_d = (
> ( 'x', 'x', 'x', 'x', 'x') ,
> ( 'x', 'x', 'x', 'x', 'x') ,
> ( 'x', 'x', 'x', 'x', 'x') ,
> ( 'x', 'x', 'x', 'x', 'x') ,
> ( 'x', 'x', 'x', 'x', 'x') ,
> );
> #much like the above example, @not_two_d is one array containing 25
> #elements. No way of knowing where one "group" ended and the next
> began.
>
> Using references:
> my @two_d = (
> [ 'x', 'x', 'x', 'x', 'x'] ,
> [ 'x', 'x', 'x', 'x', 'x'] ,
> [ 'x', 'x', 'x', 'x', 'x'] ,
> [ 'x', 'x', 'x', 'x', 'x'] ,
> [ 'x', 'x', 'x', 'x', 'x'] ,
> );
> #@two_d is now one array that contains five elements. Each element is
> a
> #reference to an array that contains five elements. We can access
> individual
> #"cells" as in $two_d[2][4] to access the 5th element of the array
> referenced by
> # the third element of @two_d
>
>
> I hope this explanation is helpful to you. If you have not already,
> please take a look at
> perldoc perllol
> perldoc perldsc
> and
> perldoc perlsub
> for more information
>
> Paul Lalli
Thanks Paul...your examples and descriptions are very helpful!
The only thing I am still unclear on is prototyping and how to define
the subroutine. For example, on:
process_arrays(\@foo, \@bar);
sub process_arrays {
my ($arr1, $arr2) = @_;
#We now have the five 'foo' values in @$arr1, and the five 'bar'
#values in @$arr2. We can use these two sets of values distinctly
#from each other.
}
would I have:
sub process_arrays(\@\@) {
# code
}
On a side note...does Prototyping have any advantages other than
enforcing calls to subroutines? Should I bother using them? Sorry for
all the questions, just trying to figure this all out.
Thanks,
Eric
.
- Follow-Ups:
- Re: subroutines, prototyping, and pass by reference question
- From: Tad McClellan
- Re: subroutines, prototyping, and pass by reference question
- From: Paul Lalli
- Re: subroutines, prototyping, and pass by reference question
- References:
- subroutines, prototyping, and pass by reference question
- From: Eric
- Re: subroutines, prototyping, and pass by reference question
- From: Gunnar Hjalmarsson
- Re: subroutines, prototyping, and pass by reference question
- From: Gunnar Hjalmarsson
- Re: subroutines, prototyping, and pass by reference question
- From: Eric
- Re: subroutines, prototyping, and pass by reference question
- From: Paul Lalli
- subroutines, prototyping, and pass by reference question
- Prev by Date: Re: subroutines, prototyping, and pass by reference question
- Next by Date: Re: subroutines, prototyping, and pass by reference question
- Previous by thread: Re: subroutines, prototyping, and pass by reference question
- Next by thread: Re: subroutines, prototyping, and pass by reference question
- Index(es):
Relevant Pages
|