conditional help

Discussion in 'Perl Misc' started by buildmorelines, Oct 3, 2004.

  1. why does this code work?

    #!/usr/bin/perl
    print "true" if $ARGV[0];

    and this doesnt and generates syntax error?

    #!/usr/bin/perl
    print "true" if $ARGV[0];
    else print "false";
    buildmorelines, Oct 3, 2004
    #1
    1. Advertising

  2. buildmorelines wrote:
    > why does this code work?
    >
    > #!/usr/bin/perl
    > print "true" if $ARGV[0];
    >
    > and this doesnt and generates syntax error?
    >
    > #!/usr/bin/perl
    > print "true" if $ARGV[0];
    > else print "false";



    I think that is simply the way perl syntax is. If you want to have an
    if-else section, you have to do something like:

    if (EXPR) { } else { }

    Check: http://www.perldoc.com/perl5.8.0/pod/perlsyn.html

    --
    Andrés
    Andres Monroy-Hernandez, Oct 3, 2004
    #2
    1. Advertising

  3. buildmorelines <> wrote:

    > and this doesnt and generates syntax error?
    >
    > #!/usr/bin/perl
    > print "true" if $ARGV[0];
    > else print "false";



    Because it is not a valid Perl program.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 3, 2004
    #3
  4. buildmorelines

    Joe Smith Guest

    buildmorelines wrote:

    > and this doesnt and generates syntax error?
    >
    > #!/usr/bin/perl
    > print "true" if $ARGV[0];
    > else print "false";


    Because perl is not C.

    if() {...} elsif() {...} elsif() {...} else {...};

    The else part in perl is surrounded by braces.

    -Joe
    Joe Smith, Oct 3, 2004
    #4
  5. buildmorelines wrote:

    > why does this code work?
    >
    > #!/usr/bin/perl
    > print "true" if $ARGV[0];


    Because this is valid Perl syntax.
    >
    > and this doesnt and generates syntax error?
    >
    > #!/usr/bin/perl
    > print "true" if $ARGV[0];
    > else print "false";


    Because this isn't Perl syntax. You don't get to
    make up stuff and tell the interpreter, "You
    know what I mean." If you want to use else,
    you have to use the compound if statement
    with blocks. For one thing, it would be
    impossible for the interpreter to correctly
    interpret where the else needs to go in nested
    if statements if this requirement wasn't
    enforced.
    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
    Chris Mattern, Oct 3, 2004
    #5
  6. buildmorelines

    Joe Smith Guest

    Abigail wrote:

    > Joe Smith () wrote on MMMMLI September MCMXCIII in
    > <URL:news:I4_7d.197410$3l3.79898@attbi_s03>:
    > :: buildmorelines wrote:
    > ::
    > :: > and this doesnt and generates syntax error?
    > :: >
    > :: > #!/usr/bin/perl
    > :: > print "true" if $ARGV[0];
    > :: > else print "false";
    > ::
    > :: Because perl is not C.
    > ::
    > :: if() {...} elsif() {...} elsif() {...} else {...};
    > ::
    > :: The else part in perl is surrounded by braces.
    >
    >
    > But
    >
    > print "true" if $ARGV [0];
    > else {print "false"}
    >
    > is a syntax either. It has nothing at all to do with "an else part
    > needs to be surrounded by braces".


    The point I was making is that when 'else' is not preceded by a '}',
    it is a syntax error. Of course the '}' has to be preceded by a '{'
    and conditional test to be a complete statement.

    You're right about the statement-modifier part; I should have
    phrased that differently.
    -Joe
    Joe Smith, Oct 11, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. itsme
    Replies:
    1
    Views:
    1,616
    Ralf Hildebrandt
    Jul 23, 2003
  2. estafford

    Help writing conditional blocks

    estafford, Aug 19, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,199
    estafford
    Aug 19, 2003
  3. CGuy
    Replies:
    4
    Views:
    561
    Saravana
    Oct 1, 2003
  4. Alec S.
    Replies:
    10
    Views:
    10,091
    Alec S.
    Apr 16, 2005
  5. Cogito
    Replies:
    4
    Views:
    345
    David Dorward
    Sep 18, 2005
Loading...

Share This Page