Re: Breaking out of an 'if'
by NateTut (Deacon) on Apr 14, 2005 at 15:26 UTC
|
How about elsif? or even else? | [reply] |
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..."
| [reply] [d/l] |
Re: Breaking out of an 'if'
by ambrus (Abbot) on Apr 14, 2005 at 15:31 UTC
|
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.)
| [reply] [d/l] |
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.
| [reply] [d/l] [select] |
|
|
Can you explain the difference between the two methods of calling?
| [reply] |
|
|
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]
| [reply] [d/l] [select] |
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();
}
| [reply] [d/l] [select] |
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).
| [reply] [d/l] |
|
|
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
| [reply] [d/l] |
|
|
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...
| [reply] |
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: {};
| [reply] [d/l] [select] |
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;
}
| [reply] [d/l] |
Re: Breaking out of an 'if'
by robot_tourist (Hermit) on Apr 15, 2005 at 10:12 UTC
|
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
| [reply] [d/l] |