Let's use a bit more modern way of writing Perl:
use strict;
use warnings;
use feature qw(say);
my @fruits = qw( apple orange pear );
for my $fruit ( @fruits ) {
if ( $fruit ne 'apple' ) {
say "$fruit is not an apple";
}
else {
say "This is an apple.";
}
}
You made several mistakes in your code, and if you had use warnings;
in your code, Perl would have told you about them. This is why use warnings
should always be included. The use strict
forces you to use stricter syntax which means that you can't use barewords (you must quote strings and make sure functions are property declared). And, you have to declare your variables (usually with my
).
You had:
foreach (@fruit) {
which means that each fruit would be in the $_
variable, but instead, you were using
if ( @fruit != ... )
In your if
statements. That is, you're comparing the entire @fruit
array to the value. Also, the !=
is for numeric comparisons only. You want ne
for string comparisons. The use warnings
would have caught both of these errors.
Your if
statements should have looked like:
if ( $_ ne '...' ) {
However, I don't usually recommend to use $_
for a variety of reasons. It's original purpose was to make the code easier to read, but it usually fails at that. Instead, I usually will use an explicit variable name:
for my $fruit ( @fruits ) {
instead of
foreach (@fruit);
Also note that I use plurals when naming arrays and hashes (since they're usually groups of objects anyway), this way, I don't end up with @fruit
, $fruit
, and %fruit
which can be confusing (although legal). You could add a suffix (i.e. @fruit_list
) to help clarify an array from the individual variable too.
I also like using say
instead of print
because say
automatically adds that \n
to the end of my printed output. I get the say
command when I add in the pragma feature qw(say);
to my program.
I also like using qw(...)
to define one word lists. It's cleaner and easier to read than using quotes and commas. Note I do not add commas between my fruit names.