Inconsistent behaviour of empty "while" EXPR

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default Inconsistent behaviour of empty "while" EXPR

    Odd one this:an empty EXPRession in a "while" loop seems to take a defined
    value in this call from either ksh on solaris 2.6 to Perl 5.004, or Cygwin
    on Win2k or Win98 to Perl 5.8.0 (ActiveState binary 806):

    perl -e 'while () { print "Aha "; }'

    because it loops endlessly, printing "Aha Aha Aha Aha . . .".
    Same behaviour if I beef this up into:

    perl -wTe 'use strict; while () { print "Aha "; }'

    or even it I put this into a file (fred), make it executable (chmod 755
    fred), and run it (perl fred).

    Contrast this with:

    perl -e 'do { print "Aha "; } while ();'

    which only prints "Aha " once, and then stops.
    Contrast still further with:

    perl -e 'if () { print "Aha "; }'

    which fails with:

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

    Note also, these print nothing, as expected:

    perl -e 'while (undef) { print "Aha "; }'
    perl -e 'while ("") { print "Aha "; }'
    perl -e 'while (0) { print "Aha "; }'
    perl -e 'while (defined) { print "Aha "; }'

    Now the Perl manual says:

    if (EXPR) BLOCK
    . . .
    LABEL while (EXPR) BLOCK
    . . .
    The while statement executes the block as long as the expression is
    true (does not evaluate to the null string "" or 0 or "0").

    Can anyone explain why my very first "while ()" looped endlessly, but the
    "do . . . while ()" stopped?
    And should I be surprised that the "if ()" gave a syntax error instead?

    Thank-you,
    Clyde






    Clyde Ingram Guest

  2. Similar Questions and Discussions

    1. Setting "URL Behaviour" to "Dynamic" has no effect
      Greetings When I set the "URL Behaviour" of a web reference an entry is created in the "Web.config", but the value in the "Web Reference URL"...
    2. Servervariables("logon_user") returns empty string
      Hi, Is the Anonymous access trued off in IIS? JN NSQUARED2
    3. Problem with DataGrid and values that are empty when AutoGenerateColumns="False"
      Hi! Im currently writing my Master Thesis in Computer Science and i have get caught in a strange DataGrid problem. Im using a DataGrid with 4...
    4. inconsistent "invalid library" message
      Hi, I have compiled a dynamic loaded PHP module and sometimes I get a "invalid library (maybe not a PHP library)" message. I just have to...
    5. "Start" "Program" "Menu" list is empty
      For what ever reason my list of installed programs in my "Start" "Programs" menu is empty. Anyone know how to restore the list. Thanks for your...
  3. #2

    Default Re: Inconsistent behaviour of empty "while" EXPR

    Clyde Ingram
    (clyde@nohamorspamgetofftheline.freeservenohamorsp am.co.uk.nohamorspam) wrote on MMMDCLXVII September MCMXCIII in <URL:news:3Jq9b.2166$966.1671@newsfep3-gui.server.ntli.net>:
    ?? Odd one this:an empty EXPRession in a "while" loop seems to take a defined
    ?? value in this call from either ksh on solaris 2.6 to Perl 5.004, or Cygwin
    ?? on Win2k or Win98 to Perl 5.8.0 (ActiveState binary 806):
    ??
    ?? perl -e 'while () { print "Aha "; }'
    ??
    ?? because it loops endlessly, printing "Aha Aha Aha Aha . . .".

    Yes. That's a feature (but poorly documented):

    $ perl -MO=Deparse -e 'while () {}'
    while (1) {
    ;
    }
    -e syntax OK

    Here's another, similar trick:

    $ perl -MO=Deparse -e 'for (;;) {}'
    while (1) {
    ;
    }
    -e syntax OK


    The latter trick comes from C.


    ?? Contrast this with:
    ??
    ?? perl -e 'do { print "Aha "; } while ();'
    ??
    ?? which only prints "Aha " once, and then stops.
    ?? Contrast still further with:
    ??
    ?? perl -e 'if () { print "Aha "; }'
    ??
    ?? which fails with:
    ??
    ?? syntax error at -e line 1, near "() "
    ?? Execution of -e aborted due to compilation errors.


    Only while () BLOCK is special. Not if () BLOCK, not do BLOCK while (),
    and neither EXPR while ().



    Abigail
    --
    $_ = "\x3C\x3C\x45\x4F\x54";
    print if s/<<EOT/<<EOT/e;
    Just another Perl Hacker
    EOT
    Abigail Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139