LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-28-2005, 03:15 PM   #1
Barca
LQ Newbie
 
Registered: Nov 2004
Location: Poland
Distribution: Slackware - best and only ;]
Posts: 29

Rep: Reputation: 15
How to break if loop in Perl?


How to break "if" checking in Perl? Let's see:

if ($cap)
{
Net::Pcap::close ($cap);
>>> HERE <<<
print "\nExit\n";
}

I'd like to add another "if" block in this. I'd like to exit from "if" checking in marked place if variable $test would be equal to 1. So, if $test =1, $cap will be closed but text "Exit" won't be displayed.
How to do that? "last" doesn't seem to work, because it's designed to use in "while" loops etc., not in "If" statement. Thanks for your help

Last edited by Barca; 07-28-2005 at 03:33 PM.
 
Old 07-28-2005, 03:21 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Yeah, if is not a loop, but a language construct. That aside, the only solution I'm aware of is the ultra-ugly "goto".
 
Old 07-28-2005, 03:42 PM   #3
Barca
LQ Newbie
 
Registered: Nov 2004
Location: Poland
Distribution: Slackware - best and only ;]
Posts: 29

Original Poster
Rep: Reputation: 15
Thx it helped I doesn't think that using goto is bad. I'd say that using it too many times isn't good, but it's not "ugly" as many people says. It's a command like any other, created to be used when necessary - like in this example
 
Old 07-28-2005, 03:48 PM   #4
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Yeah, well, there essays out there on using goto (and when to or not to).

Which makes me wonder how an optimizing compiler would handle this: (granted, this is C code)
Code:
void foo(){
    goto subarea;
    if( 0 ) /* False constant condition */
    {
        subarea:
        printf("Hit subarea.\n");
    }
}
 
Old 07-28-2005, 04:00 PM   #5
luxitan
Member
 
Registered: Aug 2003
Location: Portugal
Distribution: Gentoo
Posts: 78

Rep: Reputation: 15
Maybe I don't understand the problem but why don't you use another if:

Code:
 

if ($cap)
{
   Net::Pcap::close ($cap);
   if (!$test)
  {
     print "\nExit\n";
  }
}
 
Old 07-28-2005, 04:18 PM   #6
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
No, he seems to be desiring to break out of multiple areas, as in this code:
Code:
if($cap)
{
    Net::Pcap::close($cap);
    if(!$test){
        goto outside;
    }
    # more code
}
outside:
#even more code
 
Old 07-29-2005, 08:48 AM   #7
f76
LQ Newbie
 
Registered: Jul 2004
Location: Sweden
Distribution: Ubuntu Breezy
Posts: 20

Rep: Reputation: 0
how about else?
 
Old 08-01-2005, 03:28 PM   #8
nkthrasher
LQ Newbie
 
Registered: Aug 2005
Posts: 1

Rep: Reputation: 0
Too easy.

Code:
for(;;){
  if ($cap){
    Net::Pcap::close ($cap);
    if(wewanttobreakhere){
      last;
    }
    #insert other code here
    if(wewanttobreakhere){
      last;
    }
    print "\nExit\n";
  }
  last;
}
Cant break from the if since its not a loop, but you can put the if into a useless loop, and break from the useless loop.

Last edited by nkthrasher; 08-01-2005 at 03:42 PM.
 
Old 08-01-2005, 03:35 PM   #9
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
How about placing that within [ code] brackets?
 
Old 08-03-2011, 01:15 PM   #10
jaylink1971
LQ Newbie
 
Registered: Aug 2011
Posts: 1

Rep: Reputation: Disabled
We're all so stupid. Just do this, as suggested by "John" in another forum:

just put a loop inside the if block:

Code:
if ( $expression ) { {
 last if $oops;
 } }
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl: Where am I in a foreach loop? jrtayloriv Programming 3 01-30-2005 10:43 PM
loop through string with perl lfur Programming 2 07-03-2004 08:05 AM
Need help with perl loop! morbid_ru Programming 1 02-24-2004 01:14 PM
Need help with perl loop! morbid_ru Programming 2 02-17-2004 05:15 AM
break loop in C++ marek Programming 10 09-11-2003 10:59 AM


All times are GMT -5. The time now is 02:22 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration