Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1021759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:21:34+00:00 2026-05-16T11:21:34+00:00

I am getting this error when I use this code sub search { my

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:21:34+00:00Added an answer on May 16, 2026 at 11:21 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 541k
  • Answers 541k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try: Visible="<%# this.IsCreated %>" May 17, 2026 at 2:57 am
  • Editorial Team
    Editorial Team added an answer Unless I'm missing something, I think what you are looking… May 17, 2026 at 2:57 am
  • Editorial Team
    Editorial Team added an answer What happens in your code is that you keep opening… May 17, 2026 at 2:57 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Why am I getting this: An error occurred Application error Exception information: Message: Select
I'm getting this error (see title) while trying to parse an XML file in
I am getting this error when I try to run this MySQL command: CREATE
While developing with Firebug I keep getting this error. pages[x].css(z-index,x) is not a function
Alright, I'm having trouble getting started on this one. I'm trying to use the
I recently rebuilt my website and now I'm getting this error: Warning: mysql_result(): supplied
For some reason I am having trouble getting my head around __init__ and __new__
This is a cross post from Perl Monks and Mahalo answers, where I have
I'm having trouble getting a prepared statement in sqlite3 to work. I'm working with
I'm having a problem getting access to a database which lives on a remote

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.