Llama book exercise

P

PerseP

Hi,
I'm learning Perl by reading the "llama book". It happens that on the
answer to exercise 3 from chapter 4 (page 234), the authors ask why
the control variable in the subroutine &above_average is named
$element instead of using Perl's default $_.
The code is:

#!/usr/bin/perl

use strict;

sub total {
my $sum;
foreach(@_) {
$sum+=$_;
}
$sum;
}

sub average {
if (@_ ==0) { return; }
my $count =@_;
my $sum=&total(@_);
$sum/$count;
}

sub above_average {
my $average=&average(@_);
my @list;
my $element;
foreach $element (@_) {
if($element > $average) {
push @list,$element;
}
}
@list;
}

my @fred=&above_average(1..10);
print "\@fred is @fred\n";
print "(Should be 6 7 8 9 10)\n";
my @barney=&above_average(100,1..10);
print "\@barney is @barney\n";
print "(should be just 100)\n";

I've run the code as it's given above and with my initial answer,using
$_ instead of $elements:
sub above_average {
my $average=&average(@_);
my @list;
my $element;
foreach (@_) {
if($_ > $average) {
push @list,$_;
}
}
@list;
}

Both give the same output. So is there any point in using $element
instead of $_ as the control variable?

Thanks
 
B

brian d foy

PerseP said:
I'm learning Perl by reading the "llama book". It happens that on the
answer to exercise 3 from chapter 4 (page 234), the authors ask why
the control variable in the subroutine &above_average is named
$element instead of using Perl's default $_.

Both give the same output. So is there any point in using $element
instead of $_ as the control variable?

$_ is a global variable. Instead of getting into the bad habit of using
it when you don't need it, we suggest that you use your own variable
for the foreach. It might seem a bit silly for the simple exercise, but
it's never too early to start good coding practices. :)
 
A

anno4000

PerseP said:
Hi,
I'm learning Perl by reading the "llama book". It happens that on the
answer to exercise 3 from chapter 4 (page 234), the authors ask why
the control variable in the subroutine &above_average is named
$element instead of using Perl's default $_.
The code is:
[...]

sub above_average {
my $average=&average(@_);
my @list;
my $element;
foreach $element (@_) {
if($element > $average) {
push @list,$element;
}
}
@list;
}
[...]

I've run the code as it's given above and with my initial answer,using
$_ instead of $elements:
sub above_average {
my $average=&average(@_);
my @list;
my $element;
foreach (@_) {
if($_ > $average) {
push @list,$_;
}
}
@list;
}

Both give the same output. So is there any point in using $element
instead of $_ as the control variable?

It must be a stylistic point the authors are making there. As you
have observed, technically it is quite possible to use $_ instead.

Anno
 
P

PerseP

$_ is a global variable. Instead of getting into the bad habit of using
it when you don't need it, we suggest that you use your own variable
for the foreach. It might seem a bit silly for the simple exercise, but
it's never too early to start good coding practices. :)

Hi,

thanks for answering.The answer is a bit contradictory as there are
other examples in the book that use the $_ instead of a private
variable thus not being examples of good coding but as there are only
short examples there's nothing wrong with that.

BTW, may I notice there is a typo on that same page of the book
"Learning Perl 4th edition 0-596-10105-8 " in the same exercise where
the declaration of my $element is missing so it gives a compile error.
I've already reported it to the editor.
 
P

PerseP

$_ is a global variable. Instead of getting into the bad habit of using
it when you don't need it, we suggest that you use your own variable
for the foreach. It might seem a bit silly for the simple exercise, but
it's never too early to start good coding practices. :)


Hi,
thanks for the reply. I think it's a bit contradictory because the are
other examples in the book that use $_ as a control variable instead
of defining a new private one. As they are only examples for a book I
guess it's not that bad.

BTW there is a typo mistake in the same exercise on page 234 "Learning
Perl 4th edition 0-596-10105-8" where the declaration of the variable
"my $element" is missing thus giving a compilation error. I've already
reported it to the editor.
 
B

brian d foy

thanks for answering.The answer is a bit contradictory as there are
other examples in the book that use the $_ instead of a private
variable thus not being examples of good coding but as there are only
short examples there's nothing wrong with that.


It's not contradictory, it's just that we made an additional point in
that particular exercise. For most of the book we ignore most of the
things that we *could* talk about to focus on the thing that we want
you to learn. :)
 
P

PerseP

It's not contradictory, it's just that we made an additional point in
that particular exercise. For most of the book we ignore most of the
things that we *could* talk about to focus on the thing that we want
you to learn. :)

Ok, I understand. Thanks
 
X

Xiong Changnian

PerseP said:
I think it's a bit contradictory...

There's More Than One Way To Do It.

Of course, we all tend to react to that much freedom by saying, from
time to time:

Do It This Way Only.

Sometimes, we're right.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top