Hi Lee, On Sun, 23 Jun 2013 04:43:58 +0200 lee <l...@yun.yagibdah.de> wrote:
> James Alton <jamesalton...@gmail.com> writes: > > > lee, > > > > You have a post statement if and then a code block. You can only use one of > > two forms: > > > > print "test" if $color eq "blue"; #no parenthesis required > > if($color eq "blue"){print "test";} > > And I can't have 'last if $color eq "blue" print "test\n";'? That would > be something quite natural and a logical thing to do ... Do you want something like: if ($color eq "blue") { last; } else { print "test\n"; } Because I cannot parse the meaning of your other statement. > > > As far as last, http://perldoc.perl.org/functions/last.html > > The example given: > > > > LINE: while (<STDIN>) { > > last LINE if /^$/; # exit when done with header > > #... > > } > > > > What they're saying here is that it breaks out of a loop. Its not like a > > return where it breaks out of a subroutine, it just continues beyond the > > loop. > > Yes, I looked at that and found it rather confusing. Now I guess it > breaks out of the current (innermost) loop unless a label is being used > which would allow to break out of whatever loop the label refers to, > like: > > > BREAK: while($loop_1) { > # do something with $color and $loop_2 > while($loop_2) { > last BREAK if $color eq "blue"; > print "not blue\n"; > # do something with $color and $loop_2 > } > } > print "broken\n"; > Yes, Perl Best Practices recommends to always use a label with "last", "redo" or "next" to avoid problems where you add or remove more inner loops. I agree with it here: http://perl-begin.org/tutorials/bad-elements/#flow-stmts-without-labels Yesterday, I introduced a bug or two in my C code (which were luckily caught by the tests) because I removed a loop and as result a few C "continue" statements (which is equivalent to Perl 5’s "next") terminated the more previously outer loop. It is possible that the C compilers of http://en.wikipedia.org/wiki/GNU_Compiler_Collection and http://en.wikipedia.org/wiki/Clang have extensions for doing that similar to what Perl has, but I couldn't find them yet. > > The really confusing part is/was what the 'last' statement actually does > when used in conjunction with an 'if'. Apparently it re-defines the > meaning of 'if' --- or there are multiple versions of 'if' in perl: No, it does not. > One > version is the "usual" one like 'if(condition) { foo; } else { bar; }'. > The other one is some sort of appendix which is logically prepended > to the statement and, unfortunately, lacks the ability to do something > besides the statement it is appended to. Maybe '$color eq "blue" ? > print "test" : print "red\n" if $color eq "red";' ... > Actually, it is appended instead of prefixed, and you can do any single statement inside that, like: $x = $y if COND(); print "Hello\n" if COND(); etc. See http://perldoc.perl.org/perlsyn.html#Statement-Modifiers Also, please don't use the ternary conditional operator for side-effects, see: http://perl-begin.org/tutorials/bad-elements/#ternary_operator_instead_of_if_else > So how about: > > > { > print "123\n"; > print "456\n"; > } if $color eq "blue"; > > > That doesn't compile, so this preappended 'if' is like implemented only > half way in both directions :/ A trailing if applies only to one statement (by design). You can use: do { print "123\n"; print "456\n"; } if $color eq "blue"; but in this case I believe a normal conditional with the condition at the beginning would be preferable. Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ List of Text Processing Tools - http://shlom.in/text-proc SGlau: Sounds really crazy. Are you sure you’re OK? SMG: No, I’m not. My secret for success was that I was never completely sane, heh. — http://www.shlomifish.org/humour/Summerschool-at-the-NSA/ Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/