while ( ) and until ( )

Discussion in 'Perl Misc' started by John Nurick, Jan 16, 2006.

  1. John Nurick

    John Nurick Guest

    I'm trying to understand why 'while ()' behaves like 'while (1)', but
    'until ()' doesn't behave like 'until (1)'. So far - maybe because of
    clumsiness with perldoc - I can't find it documented.

    This compiles and runs without warnings:

    > perl -e "use warnings; while () {$x++; print $x; last if $x==9; }"

    123456789

    while this won't compile:

    > perl -e "use warnings; until () {$x++; print $x; last if $x==9; }"

    syntax error at -e line 1, near "() "
    syntax error at -e line 1, near "; }"
    Execution of -e aborted due to compilation errors.

    Could someone please point me to an explanation?

    TIA
    John
     
    John Nurick, Jan 16, 2006
    #1
    1. Advertising

  2. Also sprach John Nurick:

    > I'm trying to understand why 'while ()' behaves like 'while (1)', but
    > 'until ()' doesn't behave like 'until (1)'. So far - maybe because of
    > clumsiness with perldoc - I can't find it documented.
    >
    > This compiles and runs without warnings:
    >
    > > perl -e "use warnings; while () {$x++; print $x; last if $x==9; }"

    > 123456789
    >
    > while this won't compile:
    >
    > > perl -e "use warnings; until () {$x++; print $x; last if $x==9; }"

    > syntax error at -e line 1, near "() "
    > syntax error at -e line 1, near "; }"
    > Execution of -e aborted due to compilation errors.
    >
    > Could someone please point me to an explanation?


    It's a subtle difference in Perl's grammar:

    /* Loops: while, until, for, and a bare block */
    loop : label WHILE '(' remember texpr ')' mintro mblock cont
    { ... }
    | label UNTIL '(' remember iexpr ')' mintro mblock cont
    { ... }

    Note the distinction into 'texpr' and 'iexpr', the difference being that
    'texpr' is an expression where empty means true so 'while ()' is the
    same as 'while (1)'.

    I am quite sure this has been done on purpose because 'until () { ... }'
    would then be the same as 'until (1) { ... }', meaning: the body of the
    loop is never executed.

    Still, it would be nice to turn 'until ()' into 'until (0)' so a
    bug-report (see 'perldoc perlbug') might still be in order here.

    Tassilo
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
     
    Tassilo v. Parseval, Jan 16, 2006
    #2
    1. Advertising

  3. John Nurick

    John Nurick Guest

    On Mon, 16 Jan 2006 10:20:54 +0100, "Tassilo v. Parseval"
    <> wrote:

    >It's a subtle difference in Perl's grammar:
    >
    >/* Loops: while, until, for, and a bare block */
    >loop : label WHILE '(' remember texpr ')' mintro mblock cont
    > { ... }
    > | label UNTIL '(' remember iexpr ')' mintro mblock cont
    > { ... }
    >
    >Note the distinction into 'texpr' and 'iexpr', the difference being that
    >'texpr' is an expression where empty means true so 'while ()' is the
    >same as 'while (1)'.
    >
    >I am quite sure this has been done on purpose because 'until () { ... }'
    >would then be the same as 'until (1) { ... }', meaning: the body of the
    >loop is never executed.
    >
    >Still, it would be nice to turn 'until ()' into 'until (0)' so a
    >bug-report (see 'perldoc perlbug') might still be in order here.


    Thank you for the info. I've submitted a bug report ('wishlist'
    priority). I like the way that turning 'until ()' into 'until (0)'
    would mean that 'until ()' and 'while ()' worked the same.

    John
     
    John Nurick, Jan 16, 2006
    #3
  4. John Nurick

    John Nurick Guest

    On Tue, 17 Jan 2006 04:15:08 GMT, Tim Hammerquist
    <> wrote:

    >John Nurick <> wrote:
    >> I like the way that turning 'until ()' into 'until (0)' would
    >> mean that 'until ()' and 'while ()' worked the same.

    >
    >Which begs the inevitable question: why not just use
    >'while ()'? Just for a bit more syntactic sugar?
    >
    >Not that it wouldn't be nice, if only for symmetry's sake.
    >But can you think of any practical use?


    No - but I'm not a Perl hacker<g>.
    --
    John
     
    John Nurick, Jan 17, 2006
    #4
    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. Guest
    Replies:
    1
    Views:
    571
    Lowell Heddings
    Dec 9, 2004
  2. Replies:
    2
    Views:
    861
    Andrew Thompson
    May 22, 2005
  3. Diane Yocom
    Replies:
    0
    Views:
    545
    Diane Yocom
    Jun 21, 2006
  4. Uday Bidkar
    Replies:
    4
    Views:
    494
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  5. Logan Lee

    Difference between while and do while

    Logan Lee, Dec 24, 2007, in forum: C Programming
    Replies:
    9
    Views:
    393
    James Kuyper
    Dec 25, 2007
Loading...

Share This Page