Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: while

by perigeeV (Hermit)
on Jul 14, 2001 at 15:48 UTC ( [id://96723]=note: print w/replies, xml ) Need Help??


in reply to while without parentheses

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.

Replies are listed 'Best First'.
Re: Re: while
by wine (Scribe) on Jul 14, 2001 at 16:48 UTC
    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.
        Here's a curiosity:
        #!/usr/local/bin/perl -w use strict; $_ & 1 ? next : print "$_\n" for 1 .. 10; my $i = 0; $i & 1 ? next : print "$i\n" while ++$i <= 10; __END__ 2 4 6 8 10 Can't "next" outside a block at loops.pl line 8.
        So you can use the loop control operators with the foreach statement modifier, but not with the while statement modifier. I know that these two features were not added to Perl at the same time (perl5.004 for the while statement modifier; perl5.005 for the foreach statement modifier). Perhaps this inconsistent behavior was an oversight.

        (I don't have access to perl5.6 right now, to see if it's the same way.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://96723]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-29 13:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found