What does "exit the block or routine with a loop control operator" mean?

S

Suresh Govindachar

Hello,

Page 789 of Programming Perl says that the block or
routine used for sorting cannot be exited "with a loop
control operator". What does this mean?

Specifically, is the following OK or not-OK?

sub the_sorter
{
my $rv = 0;
for my $i(1..10)
{
# some code involving $a and $b
# an assignment to $rv

$rv and last; # exitting the loop inside a sort subroutine.
}
return $rv; # explicit return statement; not a loop-control operator
}

I suspect the preceding is indeed OK; please give an
example of exitting a "block or routine with a loop control operator".

Thanks,

--Suresh
 
B

Bob Walton

Suresh Govindachar wrote:

....
I suspect the preceding is indeed OK; please give an
example of exitting a "block or routine with a loop control operator". ....


--Suresh


They probably mean something like:


use warnings;
use strict;
sub test{
print "in test\n";
last; #terminates the for loop below
print "still in test\n";
}
for(1..3){
print "calling test $_\n";
test();
print "returned from test $_\n";
}

That gives a warning, though, at least in 5.8.4.
 
R

Richard Morse

Suresh Govindachar said:
Hello,

Page 789 of Programming Perl says that the block or
routine used for sorting cannot be exited "with a loop
control operator". What does this mean?

They are referring to exiting the direct block or subroutine you provide
to the sort operator. Not to a sub-block of this block.

IE, something like this is probably forbidden

sort {
my $a_processed = do_something($a);
my $b_processed = do_something($b);

if (!defined($a_processed) or !defined($b_processed)) {
next; # illegal!!!!
}

return $a_processed <=> $b_processed
|| $a_processed cmp $b_processed;
} @vals_to_sort;

HTH,
Ricky
 
B

Brian McCauley

Suresh Govindachar said:
Page 789 of Programming Perl says that the block or
routine used for sorting cannot be exited "with a loop
control operator". What does this mean?

Specifically, is the following OK or not-OK?

sub the_sorter
{
my $rv = 0;
for my $i(1..10)
{
# some code involving $a and $b
# an assignment to $rv

$rv and last; # exitting the loop inside a sort subroutine.
}
return $rv; # explicit return statement; not a loop-control operator
}

I suspect the preceding is indeed OK;

It is just fine.
please give an
example of exitting a "block or routine with a loop control
operator".

OK first without sort. One can use last to implement a throw/catch
type of semantic rather as you would with eval/die.

sub baz {
no warnings 'exiting';
print "last FOO\n";
last FOO;
}

sub bar {
baz();
print "Not reached\n";
}

FOO: {
bar();
print "Not reached\n";
}
print "Reached\n";

__END__

Now if instead I replace bar with

sub bar {
print sort baz 1,2,3;
}

then we get an error: Label not found for "last FOO".

IMNSHO this is a bad thing.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top