Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Can I "break out" of an if{} block? (http://www.velocityreviews.com/forums/t898430-can-i-break-out-of-an-if-block.html)

usenet@DavidFilmer.com 06-08-2006 11:40 PM

Can I "break out" of an if{} block?
 
At any point in a loop, I can "break out" of it with a 'last', or I can
"break out" of a subroutine with a 'return'.

Is there a similar way to easily "break out" of an if{} block (without
a goto)?

Thanks!

--
David Filmer (http://DavidFilmer.com)


John W. Krahn 06-08-2006 11:57 PM

Re: Can I "break out" of an if{} block?
 
usenet@DavidFilmer.com wrote:
> At any point in a loop, I can "break out" of it with a 'last', or I can
> "break out" of a subroutine with a 'return'.
>
> Is there a similar way to easily "break out" of an if{} block (without
> a goto)?


Sure, just put a loop inside the if block:

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


John
--
use Perl;
program
fulfillment

Tad McClellan 06-09-2006 12:41 AM

Re: Can I "break out" of an if{} block?
 
usenet@DavidFilmer.com <usenet@DavidFilmer.com> wrote:
> At any point in a loop, I can "break out" of it with a 'last', or I can
> "break out" of a subroutine with a 'return'.
>
> Is there a similar way to easily "break out" of an if{} block (without
> a goto)?



Yes, by nesting another if:

if ( $something ) {
# do this always
if ( ! $want_to_last ) { # unless ( $want_to_last ) is "nicer"
# do this when not "lasting"
}
}


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Ben Morrow 06-09-2006 12:45 AM

Re: Can I "break out" of an if{} block?
 

Quoth usenet@DavidFilmer.com:
> At any point in a loop, I can "break out" of it with a 'last', or I can
> "break out" of a subroutine with a 'return'.
>
> Is there a similar way to easily "break out" of an if{} block (without
> a goto)?


Any block (except for the pseudoblocks which are part of if{}, else{},
do{} &c.) is treated as a loop which loops once. So just add another set
of braces and use 'last':

if (1) {{
#...
last if #...
#...
}}

Ben

--
All persons, living or dead, are entirely coincidental.
benmorrow@tiscali.co.uk Kurt Vonnegut


All times are GMT. The time now is 09:04 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.