I am getting this error when I use this code
sub search {
my ($a, @a_list) = @_;
foreach (@a_list) {
if($_ == $a) return TRUE;
# continue;
}
return FALSE;
}
syntax error at code.pl line 26, near “) return”
- What is the right way to
return TRUE? - Also, what is the right way to
continue?
I know I should be thinking more in Perl terms than trying to convert C code to Perl this way.
Equivalents of return, continue, break
returnis the equivalent of return.nextis the equivalent of continue.lastis the equivalent of break.‘if’ statements are followed by blocks
Your problem is that an
ifstatement is always followed by a block inside braces.For all it sometimes seems a bit verbose, I agree with ysth that this is something that Perl got right. For an interesting alternative take, see the Go programming language, which treats the parentheses as unnecessary and mandates the braces.
‘if’ can be used as a qualifier
Or you can use the
ifas a statement modifier:Note that in this case, you don’t have to use parentheses around the conditional expression, though there’d be no harm in adding them.
Using ‘unless’
You can also use
unlessinstead ofifto invert the condition:(And, as Phillip Potter pointed out, mixing
unlesswith a negated condition makes comprehension harder; the example is directly doing the same as the question, but is better written as anifwith equality.)Using ‘next’ and ‘last’
You can use
nextandlastsimilarly:Note the fixup in the(Question amended to include the fixup.) Make sure you always have ‘foreachloop.use strict;‘ and ‘use warnings;‘ at the top of your script. Experts always use them to make sure they haven’t made mistakes. Beginners should use them for exactly the same reason.TRUE and FALSE
Also, as pointed out first in other answers, Perl does not have pre-defined constants TRUE and FALSE, any more than C or C++ do (C++ has a built-in
trueandfalse; C99 hastrueandfalseconditionally available if you#include <stdbool.h>). You can provide the definitions for Perl as:Be wary, though. Some things will be ‘true’ even when not equal to TRUE; other things will be ‘false’ even when not equal to FALSE.
Discussion about use of ‘$a’ and ‘$b’
The comments contain a discussion about not using
$aand$bas variables. In sequence, the relevant comments were:Please avoid using $a unless it’s in a sort block. – Zaid
@Zaid: Good point that $a and $b are special in the context of a sort block. I’m not sure whether there are edicts that they should never be used otherwise – it would be atrocious to use them when there is also a sort block lurking around, but in the absence of sort blocks, I don’t see any reason to treat $a and different than $z. – Jonathan Leffler
$a and $b are globals, and as such behave different than lexicals. – phaylon
@phaylon: well, strictly they are ‘package globals’ (see Perl sort). Yes, when you are sorting, they are different from lexicals (my) variables. When you aren’t doing sorting, then they can be treated as lexicals if you declare them explicitly. – Jonathan Leffler
@Jonathan Leffler, they are also exempt from use strict qw(vars); so you might not notice that you are trampling on them from another scope. – Ven’Tatsu
Pointing out the obvious: I only used
$abecause the question did – and rather than inundate the original poster with lots of details, I kept mostly to the main points. For example, the discussion oflastandnextdoes not mention loop labels.That said, the advice “avoid using
$aand$bas variables” is sound; they are special names for the reasons pointed out, and using them leaves open the possibility of mistakes that may or may not be be detectable.