Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the Perl equivalents of return and continue keywords in C?

Tags:

perl

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.

like image 875
Lazer Avatar asked Aug 26 '10 14:08

Lazer


2 Answers

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.

like image 146
Jonathan Leffler Avatar answered Sep 20 '22 08:09

Jonathan Leffler


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.

like image 36
mscha Avatar answered Sep 24 '22 08:09

mscha