Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

If Statement

by Baz (Friar)
on Nov 04, 2002 at 18:38 UTC ( #210239=perlquestion: print w/replies, xml ) Need Help??
Baz has asked for the wisdom of the Perl Monks concerning the following question:

Why are brackets compulsory with an If statement even if it has only one line, unlike in C?
Perl - if(a!=b) { a = b; } C - if(a!=b) a = b;

Replies are listed 'Best First'.
•Re: If Statement
by merlyn (Sage) on Nov 04, 2002 at 18:50 UTC
    1. By rule #1 {grin}. "Larry said so."
    2. There's never a chance of the "dangling else" problem.
    3. The syntax would be ambiguous without it. I forget the exact case, but apparently it'd be hard to tell for certain Perl constructs whether it was a curly-expression or the block that belongs to the if, and then you wouldn't know where the end of the block was.
    4. Perl has the "backward if" that C doesn't have:
      $simple_expression if $some_condition;

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      3. # The syntax would be ambiguous without it. I forget the exact case,...

      It's like this:

      if ( $foo ) if ( $bar ) quux(); else # ???
      Which if is that else supposed to go with?
        No, not the dangling-else problem. I already addressed that in "point 2".

        There's something that's in Perl that's not in C that would have made it hard to tell if there was a single expression or a block following. Maybe it's just the anonymous hash constructor, but back when braces were made mandatory, there was no anon-hash constructor, so I don't think that was it.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

      Re: point 3: Maybe that C doesn't have naked blocks? So would if($cond) { $foo } $bar; mean
      if($cond) { $foo } $bar;
      or is it
      if($cond) { { $foo; } $bar; }

      Makeshifts last the longest.

Re: If Statement
by kabel (Chaplain) on Nov 04, 2002 at 18:50 UTC
Re: If Statement
by fruiture (Curate) on Nov 04, 2002 at 18:51 UTC

    Because Perl is not C.

    $a = $b if $a != $b; # or if you want to stick to the order if( ) then; $a != $b and $a = $b;
    --
    http://fruiture.de
Re: If Statement
by Enlil (Parson) on Nov 04, 2002 at 18:47 UTC
    The Camel states in the C Traps section (first thing no less), that the curly brackets are required for both if and while blocks.

    -enlil

Re: If Statement
by defyance (Curate) on Nov 04, 2002 at 18:55 UTC
    Would this not work without the brackets?
    $a = $b if $a != $b;
    This is valid for such a simple operation according to the Pocket Ref. Infact, one of the first examples of control statments looks like so:
    expr1 if expr2 ; expr1 foreach list ;
    Did I read this wrong?

    UPDATE:Guess I didn't read it wrong!

    -- Can't never could do anything, so give me and inch, I'll make it a mile.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://210239]
Approved by hopes
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2018-01-18 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (206 votes). Check out past polls.

    Notices?