Newbie Perl question

Z

Zachary Turner

Hello,

I'm just learning Perl and I'm going through a book and there was an
exercise in one of the chapters to write a simple subroutine to add up
all the values that were passed as arguments. Simple enough, I
implemented this as follows:

sub total {
my $sum;

foreach (@_) {
$sum += $_;
}
return $sum;
}

However, in the same chapter it says that if you do not put a return
statement, the return value of the function is the result of the last
calculation that occured in the function. So to test this I deleted
the "return $sum;" line from the function. When the return line was
there, it returned the correct value. Without that line, it appears
to return undef.

Can anyone explain?

Thanks
 
Z

Zachary Turner

My apologies to anyone using title-threaded newsreaders such as Google
Groups, I should have chosen the title of my post more carefully :(

Zach
 
M

Michele Dondi

My apologies to anyone using title-threaded newsreaders such as Google
Groups, I should have chosen the title of my post more carefully :(

Accepted! Please also do not top post since this is highly discouraged
here.


Michele
 
M

Michele Dondi

However, in the same chapter it says that if you do not put a return
statement, the return value of the function is the result of the last
calculation that occured in the function. So to test this I deleted ^^^^^^^^^^^
^^^^^^^^^^^

the "return $sum;" line from the function. When the return line was
there, it returned the correct value. Without that line, it appears
to return undef.

Can anyone explain?

The official docs can. In fact

perldoc perlsub

says:

: If no "return" is found and if the last statement is an expression, its
: value is returned. If the last statement is a loop control structure
: like a "foreach" or a "while", the returned value is unspecified. The
: empty sub returns the empty list.

While I like the last-expression-is-returned feature, even if perl did
the "right" thing with loops, I wouldn't rely on it and use an explict
return() instead.


Michele
 
B

brian d foy

Zachary Turner said:
Hello,

I'm just learning Perl and I'm going through a book and there was an
exercise in one of the chapters to write a simple subroutine to add up
all the values that were passed as arguments. Simple enough, I
implemented this as follows:

sub total {
my $sum;

foreach (@_) {
$sum += $_;
}
return $sum;
}

However, in the same chapter it says that if you do not put a return
statement, the return value of the function is the result of the last
calculation that occured in the function. So to test this I deleted
the "return $sum;" line from the function. When the return line was
there, it returned the correct value. Without that line, it appears
to return undef.

We say in that chapter that the return value would be the "last
evaluated expression", not the last calculation. When you omit the
'return $sum', you have to decide what the last evaluated expression
is. It turns out that it's not the stuff inside the loop, but something
that foreach does on the final go around.

If you're coming out of a looping structure, don't think that the last
evaluated expression in the one in its block. For instance, consider a
while loop:

while( $foo++ < 10 ) {
$sum += $foo;
}

The last evaluated expression is always false because the last
expression is the one in the conditional. Perl has to evaluate that to
see if it should loop again. The condition is the last evaluated
expression, not the line with $sum.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top