473,748 Members | 10,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inverted syntax for an if conditional

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 donottypethisbi t com

http://markhobley.yi.org/

Apr 22 '06 #1
7 8858
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
Apr 22 '06 #2
On 2006-04-22, Mark Hobley <ma********@hot pop.deletethisb it.com> 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.
Apr 22 '06 #3
In alt.perl Mark Hobley <ma********@hot pop.deletethisb it.com> 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!1C hucky!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 donottypethisbi t com

http://markhobley.yi.org/

Apr 22 '06 #4
In alt.perl "Jürgen Exner" <ju******@hotma il.com> 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 donottypethisbi t com

http://markhobley.yi.org/

Apr 22 '06 #5
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!1C hucky!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
Apr 22 '06 #6
>>>>> "Mark" == Mark Hobley <ma********@hot pop.deletethisb it.com> 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
<me****@stonehe nge.com> <URL:http://www.stonehenge. com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.St onehenge.com for onsite and open-enrollment Perl training!
*** Posted via a free Usenet account from http://www.teranews.com ***
Apr 22 '06 #7
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/ed*******@ergn. l.hc/"
Apr 22 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5143
by: Kiki Novak | last post by:
Hi, I'm an Austrian writer living in Montpezat (South France), and I'm currently busy converting some of my writings from LaTeX to HTML to publish them on the Internet. The novel I'm working on has two different first-person narrators, formally distinguished by the use of italics. One of them (Violetta) only appears in italics, the other one (Victor) in normal fonts. This is a formal help for the reader to distinguish the two...
15
3897
by: Andy Fish | last post by:
Looking at an HTML document recently, I found this syntax which was completely new to me <!> <style>...</style> <!> it looks to be some kind of conditional logic but it's not in a javascript block, just in the normal HTML markup. none of by browsers took exception to it
177
7034
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are programming languages still being designed with C's syntax? These questions drive me insane. Every waking minute...
12
1706
by: Brad Baker | last post by:
I am trying to write a simple ASP.net/C# page which allows users to select some values and produce a report based on a SQL query. I have a self posting dropdown form which allows users to select the type of report to generate: Select the type of report to display: <form runat="server"> <asp:DropDownList AutoPostBack="true" ID="report" runat="server"> <asp:listitem>Report Type 1</asp:listitem>
5
2652
by: vd12005 | last post by:
Hello, While playing to write an inverted index (see: http://en.wikipedia.org/wiki/Inverted_index), i run out of memory with a classic dict, (i have thousand of documents and millions of terms, stemming or other filtering are not considered, i wanted to understand how to handle GB of text first). I found ZODB and try to use it a bit, but i think i must be misunderstanding how to use it even after reading...
2
4265
by: weird0 | last post by:
I have to create a string that contains inverted commas inside it. How can i do that in c#? As follows:- "AT+CMGF=1" string temp=""AT+CMGF=1""; // not valid
1
6095
by: Geosondaman | last post by:
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { int i=0, j; while (++i<10) { j=9; while (++j<=i) { printf("*");
1
1996
by: Shashank tiwari | last post by:
Hi I am trying Putting single inverted comma before a digit in Excel and a single inverted comma after the number and a comma to follow. Eg. '234567', Can anybody tell us how to do it for 7000 records Thanks
13
1549
by: Neal Becker | last post by:
In hindsight, I am disappointed with the choice of conditional syntax. I know it's too late to change. The problem is y = some thing or other if x else something_else When scanning this my eye tends to see the first phrase and only later notice that it's conditioned on x (or maybe not notice at all!). Particularly if 'some thing or other' is long or complicated.
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.