Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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;
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.


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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://210239]
Approved by hopes
[hippo]: Which version of CentOS?
[vedagiri89]: how to fix: Attempt to reload aborted
[hippo]: Ah, presumably 7 then.
[vedagiri89]: CentOS Linux release 7.0.1406 (Core)
[hippo]: How did you install libapreq2?
[vedagiri89]: that one is done by system team from the tar i guess
[hippo]: Really? Not using the one from the CentOS repository? Perhaps that's the problem.
[vedagiri89]: i need when this issue will come usually
[hippo]: How about mod_perl? Was that installed from the CentOS packge or from source?
[vedagiri89]: from the source

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2018-06-19 11:33 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (113 votes). Check out past polls.