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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-28-2005, 03:15 PM
|
#1
|
|
LQ Newbie
Registered: Nov 2004
Location: Poland
Distribution: Slackware - best and only ;]
Posts: 29
Rep:
|
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.
|
|
|
|
07-28-2005, 03:21 PM
|
#2
|
|
Moderator
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,505
Rep: 
|
Yeah, if is not a loop, but a language construct. That aside, the only solution I'm aware of is the ultra-ugly "goto". 
|
|
|
|
07-28-2005, 03:42 PM
|
#3
|
|
LQ Newbie
Registered: Nov 2004
Location: Poland
Distribution: Slackware - best and only ;]
Posts: 29
Original Poster
Rep:
|
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 
|
|
|
|
07-28-2005, 03:48 PM
|
#4
|
|
Moderator
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,505
Rep: 
|
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");
}
}
|
|
|
|
07-28-2005, 04:00 PM
|
#5
|
|
Member
Registered: Aug 2003
Location: Portugal
Distribution: Gentoo
Posts: 78
Rep:
|
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";
}
}
|
|
|
|
07-28-2005, 04:18 PM
|
#6
|
|
Moderator
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,505
Rep: 
|
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
|
|
|
|
07-29-2005, 08:48 AM
|
#7
|
|
LQ Newbie
Registered: Jul 2004
Location: Sweden
Distribution: Ubuntu Breezy
Posts: 20
Rep:
|
how about else?
|
|
|
|
08-01-2005, 03:28 PM
|
#8
|
|
LQ Newbie
Registered: Aug 2005
Posts: 1
Rep:
|
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.
|
|
|
|
08-01-2005, 03:35 PM
|
#9
|
|
Moderator
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,505
Rep: 
|
How about placing that within [ code] brackets? 
|
|
|
|
08-03-2011, 01:15 PM
|
#10
|
|
LQ Newbie
Registered: Aug 2011
Posts: 1
Rep: 
|
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;
} }
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:00 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|