Inverted syntax for an if conditional

Discussion in 'Perl' started by Mark Hobley, Apr 22, 2006.

  1. Mark Hobley

    Mark Hobley Guest

    I have some information that states that the if conditional can be be inverted
    from the traditional syntax

    if (EXPRESSION) BLOCK

    to an alternative syntax:

    if BLOCK (EXPRESSION);

    I have a simple line of code:

    if ($guess == 6) { print 'Wow! Lucky Guess!'; }

    However, when I try to invert this, I get a syntax error:

    { print 'Wow! Lucky Guess!'; } if ($guess == 6); # Syntax error

    Why does this not work?

    The example is academic, and I don't intend to code with the inverted syntax.
    I am just trying to get an understanding for the purpose of producing
    documentation.

    Thanks in advance to anyone who can help.

    Regards,

    Mark.

    --
    Mark Hobley
    393 Quinton Road West
    QUINTON
    Birmingham
    B32 1QE

    Telephone: (0121) 247 1596
    International: 0044 121 247 1596

    Email: markhobley at hotpop dot donottypethisbit com

    http://markhobley.yi.org/
    Mark Hobley, Apr 22, 2006
    #1
    1. Advertising

  2. Mark Hobley wrote:
    > I have some information that states that the if conditional can be be
    > inverted from the traditional syntax
    >
    > if (EXPRESSION) BLOCK
    >
    > to an alternative syntax:
    >
    > if BLOCK (EXPRESSION);


    Did you mean
    BLOCK if (EXPRESSION);

    Anyway, both are wrong. Why don't you check the documenation?
    From "perldoc perlsyn":

    Any simple statement may optionally be followed by a *SINGLE* modifier,
    just before the terminating semicolon (or block ending). The possible
    modifiers are:
    if EXPR

    I can only guess that this is what you were looking for.

    > However, when I try to invert this, I get a syntax error:
    > { print 'Wow! Lucky Guess!'; } if ($guess == 6); # Syntax error


    Because a block is not a simple statement. Did you try
    print 'Wow! Lucky Guess!' if ($guess == 6);

    jue
    Jürgen Exner, Apr 22, 2006
    #2
    1. Advertising

  3. Mark Hobley

    Justin C Guest

    On 2006-04-22, Mark Hobley <> wrote:
    > I have some information that states that the if conditional can be be inverted
    > from the traditional syntax
    >
    > if (EXPRESSION) BLOCK
    >
    > to an alternative syntax:
    >
    > if BLOCK (EXPRESSION);
    >
    > I have a simple line of code:
    >
    > if ($guess == 6) { print 'Wow! Lucky Guess!'; }
    >
    > However, when I try to invert this, I get a syntax error:
    >
    > { print 'Wow! Lucky Guess!'; } if ($guess == 6); # Syntax error
    >
    > Why does this not work?
    >
    > The example is academic, and I don't intend to code with the inverted syntax.
    > I am just trying to get an understanding for the purpose of producing
    > documentation.


    This works for me:

    #!/usr/bin/perl
    my $guess=6;
    print "Wow! Lucky guess!\n" if ( $guess == 6 ) ;

    (followups set)


    Justin.

    --
    Justin C, by the sea.
    Justin C, Apr 22, 2006
    #3
  4. Mark Hobley

    Mark Hobley Guest

    In alt.perl Mark Hobley <> wrote:
    >
    > { print 'Wow! Lucky Guess!'; } if ($guess == 6); # Syntax error
    >
    > Why does this not work?
    >


    I have also discovered some more weird behaviour, this time using the syntax:

    STATEMENT [, STATEMENT ], if EXPRESSION;

    If I use:

    print 'I should be so lucky!', print 'Chucky!', print 'Mucky!', print
    'Clucky!', if ($guess == 6);

    The statements run in reverse order, and I get number ones inserted in the
    output:

    Clucky!Mucky!1Chucky!1I should be so lucky!1

    Regards,

    Mark.

    --
    Mark Hobley
    393 Quinton Road West
    QUINTON
    Birmingham
    B32 1QE

    Telephone: (0121) 247 1596
    International: 0044 121 247 1596

    Email: markhobley at hotpop dot donottypethisbit com

    http://markhobley.yi.org/
    Mark Hobley, Apr 22, 2006
    #4
  5. Mark Hobley

    Mark Hobley Guest

    In alt.perl "Jürgen Exner" <> wrote:
    > Mark Hobley wrote:


    > Did you mean
    > BLOCK if (EXPRESSION);


    Oops, yes I did. That was a typing error.

    > Anyway, both are wrong. Why don't you check the documenation?
    > From "perldoc perlsyn":
    >
    > Any simple statement may optionally be followed by a *SINGLE* modifier,
    > just before the terminating semicolon (or block ending). The possible
    > modifiers are:
    > if EXPR
    >
    > Because a block is not a simple statement. Did you try
    > print 'Wow! Lucky Guess!' if ($guess == 6);
    >


    Yeah that works.

    A professional programming guide tells me that the following forms of if
    statement are legal in Perl:

    STATEMENT if EXPRESSION;

    STATEMENT, STATEMENT ... if EXPRESSION;

    BLOCK if EXPRESSION;

    Presumably there is a mistake in the text, and BLOCK if EXPRESSION should be
    omitted from this list, because STATEMENT if EXPRESSION works, but
    BLOCK if EXPRESSION appears not to.

    Regards,

    Mark.

    --
    Mark Hobley
    393 Quinton Road West
    QUINTON
    Birmingham
    B32 1QE

    Telephone: (0121) 247 1596
    International: 0044 121 247 1596

    Email: markhobley at hotpop dot donottypethisbit com

    http://markhobley.yi.org/
    Mark Hobley, Apr 22, 2006
    #5
  6. Mark Hobley wrote:
    > I have also discovered some more weird behaviour,
    > print 'I should be so lucky!', print 'Chucky!', print 'Mucky!', print
    > 'Clucky!', if ($guess == 6);
    >
    > The statements run in reverse order, and I get number ones inserted
    > in the output:
    >
    > Clucky!Mucky!1Chucky!1I should be so lucky!1


    Nothing weird at all.
    If you add explicit paranthesis then it's obvious what's going on:

    print ('I should be so lucky!',
    (print 'Chucky!',
    (print 'Mucky!',
    print ('Clucky!')
    )
    )
    )

    The injected digits '1' are just the return value 'true' of the inner
    print() statements, e.g. for the outmost you will get eventually

    print ('I should be so lucky!', 1)

    I suggest you read the documentation for the functions that you are using.
    "perldoc -f print":

    print Prints a string or a list of strings. Returns true if
    successful.


    jue
    Jürgen Exner, Apr 22, 2006
    #6
  7. >>>>> "Mark" == Mark Hobley <> writes:

    Mark> A professional programming guide tells me that the following forms of if
    Mark> statement are legal in Perl:

    Mark> STATEMENT if EXPRESSION;

    Mark> STATEMENT, STATEMENT ... if EXPRESSION;

    Mark> BLOCK if EXPRESSION;

    Mark> Presumably there is a mistake in the text, and BLOCK if EXPRESSION should be
    Mark> omitted from this list, because STATEMENT if EXPRESSION works, but
    Mark> BLOCK if EXPRESSION appears not to.

    That's really wrong.

    It's:

    EXPRESSION if EXPRESSION;

    or

    if (EXPRESSION) BLOCK

    where BLOCK is:

    { STATEMENT STATEMENT ... STATEMENT }

    and STATEMENT is:

    EXPRESSION;

    and many other things.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
    *** Posted via a free Usenet account from http://www.teranews.com ***
    Randal L. Schwartz, Apr 22, 2006
    #7
  8. Mark Hobley

    Ch Lamprecht Guest

    Mark Hobley wrote:


    > A professional programming guide tells me that the following forms of if
    > statement are legal in Perl:
    >
    > STATEMENT if EXPRESSION;
    >
    > STATEMENT, STATEMENT ... if EXPRESSION;
    >
    > BLOCK if EXPRESSION;
    >
    > Presumably there is a mistake in the text, and BLOCK if EXPRESSION should be
    > omitted from this list, because STATEMENT if EXPRESSION works, but
    > BLOCK if EXPRESSION appears not to.


    do BLOCK if EXPRESSION


    perldoc -f do

    Regards,
    Christoph
    --

    perl -e "print scalar reverse q//"
    Ch Lamprecht, Apr 23, 2006
    #8
    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. Manfred Balik

    Inverted Clock in ACEX1K

    Manfred Balik, Aug 29, 2003, in forum: VHDL
    Replies:
    1
    Views:
    1,284
    Marc Guardiani
    Aug 30, 2003
  2. Jon Maz
    Replies:
    3
    Views:
    1,625
    Jon Maz
    Feb 13, 2004
  3. Sakthi
    Replies:
    0
    Views:
    349
    Sakthi
    Sep 15, 2004
  4. Sakthi
    Replies:
    0
    Views:
    414
    Sakthi
    Sep 15, 2004
  5. AviraM
    Replies:
    2
    Views:
    6,328
    Manish Pandit
    Sep 28, 2006
Loading...

Share This Page