Re: subroutines, prototyping, and pass by reference question




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

.



Relevant Pages

  • Re: Range Object Misunderstanding
    ... So you would need to qualify your references ... The listbox is loaded from that array. ... > End Sub ... > Private Sub CommandButton2_Click ...
    (microsoft.public.excel.programming)
  • Re: DB_File (hash of array) problem
    ... But somehow the first element of the array is missing. ... You seem to be accidentally using symbolic references. ... "because the tied hash only accepts strings, ... sub write_dbf { ...
    (comp.lang.perl.misc)
  • Re: How do I Create ragged arrays in Excel VBA?
    ... I disagree with John's description of references in this instance. ... array B - a reference which won't be removed until *A* is ... Sub RaggedArray() ... ReDim A ...
    (microsoft.public.excel.programming)
  • Re: subroutines, prototyping, and pass by reference question
    ... There are three main reasons to use references: ... sub process_string { ... #we have now copied our 8 million character string ... There is no way to create, for example, a two-dimensional array without ...
    (comp.lang.perl.misc)
  • Re: subroutines, prototyping, and pass by reference question
    ... > There are three main reasons to use references: ... > sub process_string { ... > #we have now copied our 8 million character string ... It is what you do inside the subroutine, not the actual passing to the ...
    (comp.lang.perl.misc)