Why is 'last' not allowed here

R

Ronald Fischer

The program

#!/usr/bin/perl -w
use strict;
if(1)
{
last;
}

when run under perl 5.8, raises the error

Can't "last" outside a loop block at ./lasttest.pl line 5.

Note that 'man perlsyn' says:

"A BLOCK by itself (labeled or not) is semantically
equivalent to a loop that executes once. Thus you can
use any of the loop control statements in it to leave
or restart the block."

So my example above should be legal....

Ronald
 
M

Matija Papec

X-Ftn-To: Ronald Fischer

The program

#!/usr/bin/perl -w
use strict;
if(1)
{
last;
}

when run under perl 5.8, raises the error

Don't know why it isn't allowed, but you can try same thing with loop,

for (1) {
last;
#... rest of code to be skipped
}
 
B

Bart Lateur

Tassilo said:
Looks odd, but works as
expected:

if (1) {{
last; # 'next' is actually equivalent in these cases
}}

I'd prefer it the other way around:

{
if (1) {
last; # 'next' is actually equivalent in these cases
}
}
 
R

Ronald Fischer

Tassilo v. Parseval said:
That's because the blocks of 'if', 'unless' and 'do' (missed one?)
aren't treated as a block that is subject to 'last' or 'next'.

The strange thing is that the manpages explain that 'do' can't be
used, but they tell nothing about if. Just the contrary: They say
that, WRT blocks, 'if' behaves like a loop ('while', 'for').

I *see* that Perl does not treat the if-"block" as a block, but
this contradicts the documentation, isn't it? So either the implementation
or the docs are wrong in this respect.

Ronald
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top