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 ... > 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"; 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: 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";' ... 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 :/ > I think this fixes your program: > > > use strict; > use warnings; > use autodie; > > my $counter = 0; > > while($counter < 8) { > if($counter > 2) { > print "if: " . $counter . "\n"; > last; > } > else { > print "else: " . $counter . "\n"; > } > $counter++; > } Yes, it gives the same output as ... use strict; use warnings; use autodie; my $counter = 0; while($counter < 8) { next if($counter > 2); print "else: " . $counter . "\n"; } continue { if($counter > 2) { print "if: " . $counter . "\n"; last; } $counter++; } ... and is more efficient :) -- "Object-oriented programming languages aren't completely convinced that you should be allowed to do anything with functions." http://www.joelonsoftware.com/items/2006/08/01.html -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/