A non-loop block early exit?

S

Sara

Can I exit a block early, as "last" operator does in a loop? Such as:


unless ($catsHaveWings)
{..do stuff
"escape" if $dogsHaveFins;
.. do more stuff
}
# I want "escape" to reenter HERE
 
J

James Willmore

Can I exit a block early, as "last" operator does in a loop? Such as:


unless ($catsHaveWings)
{..do stuff
"escape" if $dogsHaveFins;
.. do more stuff
}
# I want "escape" to reenter HERE
.
.
.

I tried next and last operators and predictably they jump out of the
whole sub, not just the block or bareblock.

One option is to put the block into a subroutine and use 'return'.

sub check_animal{
unless ($catsHaveWings)
{..do stuff
return undef if $dogsHaveFins;
.. do more stuff
}
return 1;
}

This allows you to also evaluate the 'return' value and then do something
else in your main script.

For eaxmple
.....
print "Cats do have wings\n" if check_animal();
....

-or-
....
if(defined check_animal()){
print "Animals\n";
}else{
print "Zoo is empty\n";
}
....

That's just one way to do it.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Make it myself? But I'm a physical organic chemist!
 
G

Gunnar Hjalmarsson

Sara said:
Can I exit a block early, as "last" operator does in a loop?
Such as:

unless ($catsHaveWings)
{..do stuff
"escape" if $dogsHaveFins;
.. do more stuff
}
# I want "escape" to reenter HERE

You can do:

unless ($catsHaveWings) {
MYBLOCK: {
..do stuff
last MYBLOCK if $dogsHaveFins;
.. do more stuff
}
}
 
B

Brad Baxter

You can do:

unless ($catsHaveWings) {
MYBLOCK: {
..do stuff
last MYBLOCK if $dogsHaveFins;
.. do more stuff
}
}

And FWIW, the extra block need not be named, though it's arguably clearer
that way ...

my $catsHaveWings = 0;
my $dogsHaveFins = 1;

unless ($catsHaveWings) {{
#..do stuff
print "doing stuff\n";

#"escape" if $dogsHaveFins;
last if $dogsHaveFins;

#.. do more stuff
print "doing more stuff\n";
}}

# I want "escape" to reenter HERE
print "I'm HERE now.\n";

__END__
doing stuff
I'm HERE now.

Regards,

Brad
 
D

David K. Wall

Sara said:
Can I exit a block early, as "last" operator does in a loop? Such as:


unless ($catsHaveWings)
{..do stuff
"escape" if $dogsHaveFins;
.. do more stuff
}
# I want "escape" to reenter HERE

You're already using one 'unless' block. Why not two?

unless ($catsHaveWings) {
# do stuff here
print "Doing stuff\n";
unless ($dogsHaveFins) {
print "Doing more stuff\n";
}
}


If you *really* need it, Perl does have goto, although I wouldn't use it to
jump more than a few lines if I used it at all. More explicitly: goto can
turn code into a mass of spaghetti and so should be used sparingly at most.
("You have entered a maze of twisty GOTOs, all alike...")


unless ($catsHaveWings) {
print "2 Doing stuff\n";
goto DOGS_HAVE_FINS if $dogsHaveFins;
#.. do more stuff
print "2 Doing more stuff\n";
}
DOGS_HAVE_FINS:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top