Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Newbie Perl question

Reply
Thread Tools

Newbie Perl question

 
 
Zachary Turner
Guest
Posts: n/a
 
      08-19-2007
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

 
Reply With Quote
 
 
 
 
Zachary Turner
Guest
Posts: n/a
 
      08-19-2007
My apologies to anyone using title-threaded newsreaders such as Google
Groups, I should have chosen the title of my post more carefully

Zach

On Aug 19, 2:04 pm, Zachary Turner <divisorthe...@gmail.com> wrote:
> 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



 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      08-19-2007
On Sun, 19 Aug 2007 19:07:27 -0000, Zachary Turner
<> wrote:

>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      08-19-2007
On Sun, 19 Aug 2007 19:04:11 -0000, Zachary Turner
<> wrote:

>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
brian d foy
Guest
Posts: n/a
 
      08-20-2007
In article < .com>,
Zachary Turner <> wrote:

> 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.

--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 04-04-2011 10:00 PM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 02-27-2011 11:00 PM
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57