Hi lee,
Please, check my comment below:

On Sun, Jun 23, 2013 at 3:43 AM, 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 ...
>
> logical? How is that? Wrap up, the function 'print' and 'last' in the if
statement like so:
  if( $color eq "blue"){
     print "test\n";
     last LABEL;
  }
NOTE: That when last is used the loop in question is immediately exits
and execution start at the LABEL statement if provided. So any code after
the last function is not disregarded.

For example:
<CODE>
use warnings;
use strict;

  for( qw( rougue blac voir vervet)){
    print $_,$/;
    last if $_ eq 'voir';
    print '1 2 3',$/
  }
</CODE>
Which produces this:

rougue
1 2 3
blac
1 2 3
voir


 The color is been printed, then 1 2 3, BUT immediately the if condition is
met, the printing of 1 2 3 was disregarded. Of course is best practice to
use LABEL with last function.

> As far as last, http://perldoc.perl.org/functions/last.html
>

Please check this link again....

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


Which is?



>
> --
> "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/
>
>
>


-- 
Tim

Reply via email to