Newbie Perl question

Discussion in 'Perl Misc' started by Zachary Turner, Aug 19, 2007.

  1. 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
     
    Zachary Turner, Aug 19, 2007
    #1
    1. Advertising

  2. 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 <> 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
     
    Zachary Turner, Aug 19, 2007
    #2
    1. Advertising

  3. 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,
     
    Michele Dondi, Aug 19, 2007
    #3
  4. 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,
     
    Michele Dondi, Aug 19, 2007
    #4
  5. Zachary  Turner

    brian d foy Guest

    In article <>,
    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
     
    brian d foy, Aug 20, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,863
  2. Jerry C.
    Replies:
    8
    Views:
    276
    Uri Guttman
    Nov 23, 2003
  3. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    333
    PerlFAQ Server
    Jan 23, 2011
  4. PerlFAQ Server
    Replies:
    0
    Views:
    727
    PerlFAQ Server
    Feb 3, 2011
  5. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Feb 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    323
    PerlFAQ Server
    Feb 27, 2011
Loading...

Share This Page