A pox on gmail's "reply" not sending to list. 2009/12/22 Erez Schatz <moonb...@gmail.com>: > 2009/12/21 Vishnu <chadavis...@gmail.com>: >> I was going through the book intermediate perl and came across the >> following code. > > I'm not familiar with the scope of Intermediate Perl, but from your > questions it would seem that you should've started with Learning Perl. > At any rate, both lines you highlight include "perl idioms", i.e. > usage of syntax that is immediately recognised by any programmer > sufficiently fluent in the language. > >> my $input = shift; ------------> what are we doing here? > > a bare shift performs a shift() > (http://perldoc.perl.org/functions/shift.html) action on the default > array, which inside a sub is made of all the parameters that have been > passed to the sub. In this case, the function is digit_sum_is_odd($_), > meaning $_ is the parameter passed on to digit_sum_is_odd(). the first > line is assigning the parameter to $input. > > The actual reasoning behind this can be found here: > http://perldoc.perl.org/perlsub.html, and, most likely, in Learning > Perl. > >> $sum += $_ for @digits; ---------> what exactly is the > > In Perl, we pride ourself that There Is More Than One Way To Do It. > And this often means that there is more than one way to write stuff in > a way that makes more sense in the context of the specific line. This > line basically means "iterate over all items in the array @digits, and > add them to $sum", and is a way more simple way of writing: > > for (my $i; $i < @digits; i++) { > $sum += $digits[$i]; > } > > At any rate, it appears that Intermediate Perl does assume a certain > familiarity with Perl idioms, so you may encounter many more of these > throughout it. I do suggest getting Learning Perl, at least as a > reference for these.
-- Erez "The government forgets that George Orwell's 1984 was a warning, and not a blueprint" http://www.nonviolent-conflict.org/ -- http://www.whyweprotest.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/