awohld has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking at this code in a book:
$maximum = &max(3, 5, 10, 4, 6); sub max { my($max_so_far) = shift @_; foreach (@_) { if ($_ > $max_so_far) { $max_so_far = $_; } } $max_so_far; }
So line 4: "my($max_so_far) = shift @_;" is a "$max_so_far" list variable? If so why is it a list variable, just because it's in parenthesis? Could it just have been typed as "my $max_so_far" and just let it be a scalar?

And line 10: "$max_so_far", is that just there so it is returned as the return value?

If it wasn't there, would the return value be the last boolean comparison made in the "if" statement?

Replies are listed 'Best First'.
Re: Need to check my understanding of this code...
by davidrw (Prior) on Sep 04, 2005 at 19:12 UTC
    line 4: yes, your assumption is correct -- $max_so_far is a scalar .. These two lines are equivalent:
    my($max_so_far) = shift @_; my $max_so_far = shift @_;
    You can, however, declare several scalars at the same time, in which case you use the parens:
    my ($x, $y, $x) = (1, 2, 3);
    As for line 10, yes, that last line does the same as return $max_so_far; (which would have been more explicit). From perldoc -f perlsub: "The return value of a subroutine is the value of the last expression evaluated."
    Trying it with line 10 commented out, $maximum is undef .. i tried w/the max value last in the list, too, so i supsect it's not the last "if", but that the undef comes from the loop termination..
Re: Need to check my understanding of this code...
by borisz (Canon) on Sep 04, 2005 at 19:04 UTC
    line 4 gets the first entry from the list. It does the same as my $max_so_far = shift. And $max_so_far is the last statement and returned to the calling sub. So it is like return $max_so_far;
    Boris

      Not quite. Shift removes the first entry from the list, a simple assignment doesn't (although since that list is never used again, in this context it makes no difference - except that the assignment is slightly faster). Compare:

      #!/usr/bin/perl use strict; use warnings; my @list = qw/a b c/; my ($x) = @list; print "X: $x, List: @list\n"; my $y = shift @list; print "Y: $y, List: @list\n";

      The output being:

      X: a, List: a b c
      Y: a, List: b c
      
        ???, I think one of us misread the question. I talk about the difference from my $x = shift; and my ($x) = shift @_; sure, there is a difference, but not in the context of the question.
        Boris
Re: Need to check my understanding of this code...
by merlyn (Sage) on Sep 04, 2005 at 19:23 UTC
    this code in a book
    My, that text looks very familiar. Are you using Learning Perl 3rd edition, or 4th edition?

    As for your questions... I thought we addressed both of those points fairly head on in the surrounding text. Are you reading the text and doing the exercises, or just skipping around wondering about things?

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      I'm using the 4th edition. I'm reading straight through but only get an hour here or there, I'd like to do 100% of the exercises but I just don't always have computer access when I'm reading.

      And you did address it in the surrounding text. I had to stop reading right after that exercise to change some diapers (not mine) and got most of my questions answered in the follow up paragraph.

      You're book is great, I bought some others that aren't 1/4 as good.

      I really like how you show 4 or 5 varying examples of what you're explaining.

      I think everyone learning Perl must read your book.