tags:

views:

125

answers:

4

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.

+17  A: 

Equivalents of return, continue, break

  • return is the equivalent of return.
  • next is the equivalent of continue.
  • last is the equivalent of break.

'if' statements are followed by blocks

Your problem is that an if statement is always followed by a block inside braces.

if ($_ == $a) { return TRUE; }
elsif (...)   { ... }
else          { ... }

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 if as a statement modifier:

return TRUE if $_ == $a;

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 unless instead of if to invert the condition:

return TRUE unless $_ != $a;

(And, as Phillip Potter pointed out, mixing unless with a negated condition makes comprehension harder; the example is directly doing the same as the question, but is better written as an if with equality.)

Using 'next' and 'last'

You can use next and last similarly:

sub search {
    my ($a, @a_list) = @_;
    foreach (@a_list) {
        return TRUE if $_ == $a;
        last if $_ > $a;
        next if $ptom != LAST_QUARTER;
        ...
    }
    return FALSE;
}

Note the fixup in the foreach loop. (Question amended to include the fixup.) Make sure you always have '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 true and false; C99 has true and false conditionally available if you #include <stdbool.h>). You can provide the definitions for Perl as:

use constant TRUE => 1;
use constant FALSE => 0;

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 $a and $b as 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 $a because 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 of last and next does not mention loop labels.

That said, the advice "avoid using $a and $b as 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.

Jonathan Leffler
...but don't ever use `unless` with a negative condition such as one which uses `!` or `!=`. It's the code equivalent of a double negative, and it just confuses people.
Philip Potter
Please avoid using `$a` unless it's in a `sort` block.
Zaid
@Philip: Yes - agreed in general; I was just keeping the condition consistent, which maybe wasn't the best choice. Certainly, in the context, there is no call for 'unless'.
Jonathan Leffler
@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
"next" and "last" are definitely not the same thing.
Joris Meys
@Zaid: thanks for mentioning. I had no idea.
Lazer
always requiring {} for if statements is one of the things Perl got right
ysth
@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](http://perldoc.perl.org/functions/sort.html)). 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
A: 

As far as I know, TRUE and FALSE don't exist in Perl. Try

sub search {
    my ($a, @a_list) = @_;
    foreach (@a) {
        if($_ == $a) {return 1};
    }
    return;
}

What do you mean with the right way to continue?

Joris Meys
Please avoid using `$a` unless it's in a `sort` block.
Zaid
I meant to ask the correct equivalent of the `continue` keyword in C.
Lazer
Indeed, I just copy-pasted the code of the OP.
Joris Meys
@lazer : "next" is the Perl equivalent of "continue" in C
Joris Meys
`use constant TRUE => 1;` and `use constant FALSE => 0;` would provide definitions of TRUE and FALSE. Of course, neither TRUE nor FALSE exists in C code either - unless you do some similar definition (either with `enum` or `#define`). And yes, then you get into debates about the safety of the comparing values with TRUE or FALSE.
Jonathan Leffler
IIRC TRUE and FALSE are included by about the 400th file included from `windows.h`.
Ven'Tatsu
+3  A: 

Perl does not have built in constants for true and false. The canonical value for true is 1 and for false is () (which is an empty list in list context, and undef in scalar context).

A more idomatic way to write your code would be:

sub search {
    my $key = shift;
    foreach (@_) {
        return 1 if $_ == $key;
    }
    return ();
}
Eric Strom
+6  A: 

Your question has been (very!) comprehensively answered by Jonathan, but I wanted to point out a few other, more perlish ways to replace your "search" function.

use strict;
use warnings;
use 5.010;

my @a_list = (1, 2, 3);
my $a = 2;

# If you're using 5.10 or higher, you can use smart matching.
# Note that this is only equivalent if $a is a number.
# That is, "2.0" ~~ ["1", "2", "3"] is false.
say $a ~~ @a_list;

# Need to install List::MoreUtils, not a standard module
use List::MoreUtils qw(any);
say any { $a == $_ } @a_list;

# List::Util is a core module
use List::Util qw(first);
say defined first { $a == $_ } @a_list;

# No modules used, but less efficient
say grep { $a == $_ } @a_list > 0;

For more info, see the documentation for smart matching, List::MoreUtils, List::Util and grep.

mscha