A non-loop block early exit?

Discussion in 'Perl Misc' started by Sara, Feb 10, 2004.

  1. Sara

    Sara Guest

    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
     
    Sara, Feb 10, 2004
    #1
    1. Advertising

  2. On Tue, 10 Feb 2004 06:30:48 -0800, Sara wrote:

    > 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!
     
    James Willmore, Feb 10, 2004
    #2
    1. Advertising

  3. Sara wrote:
    > 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
    }
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 10, 2004
    #3
  4. Sara

    Brad Baxter Guest

    On Tue, 10 Feb 2004, Gunnar Hjalmarsson wrote:

    > Sara wrote:
    > > 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
    > }
    > }


    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
     
    Brad Baxter, Feb 10, 2004
    #4
  5. Sara <> wrote:

    > 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:


    --
    David Wall
     
    David K. Wall, Feb 10, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Miles

    Exit Custom Server Control Early

    Marc Miles, Aug 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    355
    Marc Miles
    Aug 17, 2005
  2. Jonathan Bartlett

    Using exceptions for early exit

    Jonathan Bartlett, Dec 12, 2005, in forum: Java
    Replies:
    37
    Views:
    1,162
    Oliver Wong
    Dec 16, 2005
  3. chad
    Replies:
    14
    Views:
    582
    Arnaud Delobelle
    Oct 13, 2010
  4. Suresh Govindachar
    Replies:
    3
    Views:
    94
    Brian McCauley
    Jul 27, 2004
  5. Isaac Won
    Replies:
    9
    Views:
    387
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page