Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Why doesn't this work without the parenthesis around <*.*>?
while (<*.*>) { print; print "\n"; }

Replies are listed 'Best First'.
Re: while
by perigeeV (Hermit) on Jul 14, 2001 at 15:48 UTC
    The conditional test of a statement(while, for,if) needs to be partitioned within parenthesis simply because the parser needs to know where the test statement begins and ends. It usually wouldn't matter since most of the time the openning bracket { marks the end of the test, but this isn't always true. For instance you can insert another statement that requires brackets within the conditional test:

    while( sub{return $foo} ) {...code...}

    Perl is wildly free form, but you've got to have some syntactic rules.

      The following works fine without parentheses, but also in this case is the end of the conditional test of the while statement very clear, i.e. ;

      print "$_\n" while <*.*>;

        You're right of course, but then the while is a modifier to a simple statement. Modifiers are slightly different creatures, for instance you can't use loop control operators or labels with modifiers.
Re: while
by petdance (Parson) on Jul 14, 2001 at 22:26 UTC
    Define "doesn't work." What platform are you on? Are you on Unix and you're not getting files that don't have dots in the filename?

    Or are you in an endless loop because you're using a while instead of a for?

    xoxo,
    Andy
    --
    <megaphone> Throw down the gun and tiara and come out of the float! </megaphone>

      %(~)>perl testwhile syntax error at t line 1, near "while <*.*>" syntax error at t line 4, near "}" Execution of t aborted due to compilation errors. %(~)>
      I get that response when I try to run the script without the parenthesis around the <*.*>
        (Be warned, newbie here) I was under the impression that the literal definition of a while structure in Perl was defined in the parser as the while keyword-a parenthesized statement (that is itself further defined as any returning object, or some such) and a loop body that is delimited by curlies. Of course, there is a little rider to that that states that in some cases exceptions can be made assuming that there is no ambiguity in the current context. I would imagine that there is some ambiguity associated with a while <*.*> construct in the parser...otherwise I have been told that it would work. It could be a precedence problem, considering that the parens kick the precedence for the token up quite a bit. Course, I'm a newbie so take all of this for what you paid for it MSemich
        The issue is one of syntax.

        Perl's syntax expects the body of the while conditional to be wrapped in parenthesis. This allows it to be read by others, including the interpreter. Every language has a syntax to make it understandable to multiple people. The English language, for example expects the first character of each sentence to be capitalized, a period (or some other form of punctuation...) at the end of each sentence, and spaces between words.

        itdbealothardertoreadifiwrotelikethis!!!