Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
Welcome to the Monastery
 
PerlMonks  

Breaking out of an 'if'

by jpfarmer (Pilgrim)
on Apr 14, 2005 at 15:23 UTC ( [id://447922]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

jpfarmer has asked for the wisdom of the Perl Monks concerning the following question:

I frequently find myself in a situation where I have code that looks like this:

if ($some_condition){ &some_stuff(); # stop here if a condition is met &some_more_stuff(); }

Frequently, when I get to the line where the comment is, I'd like to skip over some_more_stuff() and continue with the program. However, I can't seem to find a graceful way to do it. last seems to be erratic with if blocks.

I appreciate any assistance you can offer.

Replies are listed 'Best First'.
Re: Breaking out of an 'if'
by NateTut (Deacon) on Apr 14, 2005 at 15:26 UTC
    How about elsif? or even else?
Re: Breaking out of an 'if'
by DrWhy (Chaplain) on Apr 14, 2005 at 15:31 UTC
    The most obvious way I can think of is:

    if ($some_condition) { some_stuff(); unless (condition_is_met()) { some_more_stuff(); } }

    --DrWhy

    "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

Re: Breaking out of an 'if'
by ambrus (Abbot) on Apr 14, 2005 at 15:31 UTC

    You can try this.

    STUFF: { if ($some_condition) { &some_stuff(); &condition_met() and last STUFF; &some_more_stuff; } } # last STUFF jumps here

    The return statment can also be a useful shortcut in many cases when you want to do a non-local exit. (The return statement returns from the innermost sub, whether named or anonymous.)

Re: Breaking out of an 'if'
by ikegami (Patriarch) on Apr 14, 2005 at 15:34 UTC
    if ($some_condition) { some_stuff(); if (!$some_condition2) { some_more_stuff(); } }

    or

    if ($some_condition) { some_stuff(); } if ($some_condition && !$some_condition2) { some_more_stuff(); }

    or

    IF: { if (some_condition) { some_stuff(); last IF if $some_condition2; some_more_stuff(); } }

    btw, don't use & on function calls unless you must. It causes Perl to behave differently if the function has a prototype.

      Can you explain the difference between the two methods of calling?

        The prototype is ignored when & is used.

        use strict; use warnings; sub testing(\@) { my ($arg) = @_; print("First argument: ", $arg, "\n"); $arg->[5] = 'e'; } my @a = qw( a b c d ); print("Test 1:\n"); testing(@a); print("\n"); print("Test 2:\n"); &testing(@a); print("\n"); __END__ output ====== Test 1: First argument: ARRAY(0x1ab2780) Test 2: First argument: a Can't use string ("a") as an ARRAY ref while "strict refs" in use at ! +.pl line 8 .

        It also varies the behaviour of function calls with no parens:

        sub testing { print("[@_]\n"); } sub test1 { testing; } sub test2 { &testing; } print("Test 1:\n"); test1(); print("\n"); print("Test 2:\n"); test2('moooo'); print("\n"); __END__ output ====== Test 1: [] Test 2: [moooo]
Re: Breaking out of an 'if'
by dmorelli (Scribe) on Apr 14, 2005 at 15:50 UTC
    If you can make some_stuff() return something true or false, you could use &some_stuff() && &some_more_stuff() like this:

    #! /usr/bin/perl -w use strict; # Just to test, pass in 1 or 0 from the shell sub some_stuff { $ARGV[0]; } sub some_more_stuff { print "Got to some_more_stuff()\n"; } if(1) { &some_stuff() && &some_more_stuff(); }
Re: Breaking out of an 'if'
by bluto (Curate) on Apr 14, 2005 at 16:26 UTC
    One more old chestnut solution...

    if ($some_condition){{ &some_stuff(); last if $another_condition; &some_more_stuff; }}

    ... though I don't tend to use this since I find the extra braces to be too subtle when rereading the code (i.e. someone, ok me, removes them since they look redundant which then changes the sematics of the surrounding code).

      I find the extra braces to be too subtle when rereading the code
      Whitespace is your friend:
      if ($some_condition){ { #heck, even comment the bare block &some_stuff(); last if $another_condition; &some_more_stuff; } }

      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      For all of us waiting, your kingdom will come

        Whitespace is your friend:

        That helps some, but to me it still looks like a cut/paste error without a comment. If you have to comment, I'm not sure what the point is (i.e. use something like one of the other solutions which is more self-describing), but of course YMMV. Now if you use this all of the time in the code you maintain then that's probably fine since it becomes an idiom (i.e. a programmer will see this code multiple times and realize it isn't a typo). I find that I rarely if ever do this so it would become more of a hazard than anything else to me...

Re: Breaking out of an 'if'
by jhourcle (Prior) on Apr 14, 2005 at 18:53 UTC

    Two more options for you, depending on how much external info you need to make use of:

    somefunction() if $some_condition; sub somefunction { &some_stuff(); return if $condition; &some_more_stuff(); }

    Or, the one that I'll probably get flamed for:

    if ($some_condition){ &some_stuff(); goto EXIT if $condition; &some_more_stuff(); } EXIT: {};
Re: Breaking out of an 'if'
by holli (Abbot) on Apr 15, 2005 at 06:48 UTC
    why not abuse the while-loop?
    while ($some_condition) { &some_stuff(); last if $another_condition; &some_more_stuff; last; }


    holli, /regexed monk/
Re: Breaking out of an 'if'
by robot_tourist (Hermit) on Apr 15, 2005 at 10:12 UTC

    How about the really simple route:

    if ($some_condition) { some_stuff(); if (!($condition_met)) { some_more_stuff(); } }

    How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist.
    Robot Tourist, by Ten Benson

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://447922]
Approved by DrWhy
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.