For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2006 > about return value









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author about return value
Practical Perl

2006-03-25, 3:57 am

Hi,list,

When a subroutine return a value,for example,this value is a pointer to an
array,which is get defined in this subroutine.When out of the
subroutine,some a statement call this subroutine and receive the return
value to a variable named as $aaa.Is $aaa a duplicate of the pointer in that
subroutine?Thanks.

sub test {
@abc = qw/aa bb cc dd/;
return \@abc;
}

my $aaa = test( );

Mr. Shawn H. Corey

2006-03-25, 7:57 am

Practical Perl wrote:
> Hi,list,
>
> When a subroutine return a value,for example,this value is a pointer to an
> array,which is get defined in this subroutine.When out of the
> subroutine,some a statement call this subroutine and receive the return
> value to a variable named as $aaa.Is $aaa a duplicate of the pointer in that
> subroutine?Thanks.
>
> sub test {
> @abc = qw/aa bb cc dd/;
> return \@abc;
> }
>
> my $aaa = test( );
>


It's not a pointer, it's a reference. They work differently than
pointers. In the above, $aaa is a reference to the array @abc, which,
btw, is a global. Had you written:

sub test {
my @abc = qw/aa bb cc dd/; # scoped inside the subroutine
return \@abc;
}

my $aaa = test();

__END__

Now it looks like $aaa is a reference to an array which is out of scope
but Perl keeps a count of all references to a thingy and only when this
count reaches zero does it delete them.

So if you:
print "@$aaa\n";
you will see:
aa bb cc dd
on the output.


--

Just my 0.00000002 million dollars worth,
--- Shawn

"For the things we have to learn before we can do them,
we learn by doing them."
Aristotle

"The man who sets out to carry a cat by its tail learns something that
will always be useful and which will never grow dim or doubtful."
Mark Twain

"Believe in the Divine, but paddle away from the rocks."
Hindu Proverb

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/

Paul Lalli

2006-03-27, 6:57 pm

Jay Savage wrote:
> On 3/25/06, Practical Perl <practicalperl@gmail.com> wrote:
[color=darkred]
>
> No. $aaa is not a duplicate of @abc, it's a refernce to @abc. If you
> change $aaa, @abc will change, too, and vice versa.


No. Changes to $aaa do not affect @abc, nor vice-versa.

my @abc = qw/aa bb cc dd/;
my $aaa = \@abc;
print "ABC: @abc, AAA: $aaa\n";
push @abc, 'foo';
print "ABC: @abc, AAA: $aaa\n";
$aaa = [6, 7, 8];
print "ABC: @abc, AAA: $aaa\n";
__END__

Output:
ABC: aa bb cc dd, AAA: ARRAY(0x14dfe4)
ABC: aa bb cc dd foo, AAA: ARRAY(0x14dfe4)
ABC: aa bb cc dd foo, AAA: ARRAY(0x14e1b8)

$aaa stayed the same between the first and second prints, even though
@abc changed. @abc stayed the same between the second and third
prints, even though $aaa changed.


A similar statement that *is* correct, however, would be: "If you
modify the array that $aaa references, @abc will change, and
vice-versa"....

my @abc = qw/aa bb cc dd/;
my $aaa = \@abc;
print "ABC: @abc, AAA's array: @{$aaa}\n";
push @abc, 'foo';
print "ABC: @abc, AAA's array: @{$aaa}\n";
@{$aaa} = (6, 7, 8);
print "ABC: @abc, AAA's array: @{$aaa}\n";
__END__

Output:
ABC: aa bb cc dd, AAA's array: aa bb cc dd
ABC: aa bb cc dd foo, AAA's array: aa bb cc dd foo
ABC: 6 7 8, AAA's array: 6 7 8

Terminology is important, even more so when dealing with references.

Paul Lalli

Chas Owens

2006-03-27, 9:57 pm

On 3/27/06, Practical Perl <practicalperl@gmail.com> wrote:
> Thanks all.
> Again,how about the difference about a pointer and a reference?


Perl does Grabage Collection (cleaning up unused memory) for you. It
does this by keeping a count of the number of references a block of
memory has. When this count reaches zero the memory can be marked as
unused. If Perl had C like pointers they could be considered weak
references (ones that did not increment the number of references to
the block memory). Luckily it is quite hard to achieve that effect in
Perl (it is possible) since it would cause the same sort of problems
pointers cause in C (pointing to blocks of unused memory, confusion
about what is being pointed to, null pointers, etc.).

For more information you should read

http://perldoc.perl.org/perlreftut.html
http://perldoc.perl.org/perlref.html

or

perldoc perlreftut
perldoc perlref
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com