Why is 'last' not allowed here

Discussion in 'Perl Misc' started by Ronald Fischer, Jul 16, 2003.

  1. 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
     
    Ronald Fischer, Jul 16, 2003
    #1
    1. Advertising

  2. Ronald Fischer

    Matija Papec Guest

    X-Ftn-To: Ronald Fischer

    (Ronald Fischer) wrote:
    >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
    }



    --
    Matija
     
    Matija Papec, Jul 16, 2003
    #2
    1. Advertising

  3. Ronald Fischer

    Bart Lateur Guest

    Tassilo v. Parseval wrote:

    >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
    }
    }

    --
    Bart.
     
    Bart Lateur, Jul 16, 2003
    #3
  4. Bart Lateur <> wrote:

    > Tassilo v. Parseval wrote:
    >
    >>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
    > }
    > }


    I like this even better:
     
    David K. Wall, Jul 16, 2003
    #4
  5. "Tassilo v. Parseval" <> wrote in message news:<bf353t$8e6$-Aachen.DE>...
    > 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
     
    Ronald Fischer, Jul 17, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joshua Beall

    Character data not allowed here? What?

    Joshua Beall, May 1, 2004, in forum: HTML
    Replies:
    19
    Views:
    16,069
    Mark Parnell
    May 3, 2004
  2. mike
    Replies:
    1
    Views:
    457
    Jukka K. Korpela
    May 2, 2004
  3. D. Alvarado

    complexType is not allowed here!?

    D. Alvarado, Apr 20, 2004, in forum: XML
    Replies:
    2
    Views:
    863
    Henry S. Thompson
    Apr 21, 2004
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,030
    Smokey Grindel
    Dec 2, 2006
  5. Iñaki Baz Castillo
    Replies:
    13
    Views:
    509
    Iñaki Baz Castillo
    May 1, 2011
Loading...

Share This Page