Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > conditional help

Reply
Thread Tools

conditional help

 
 
buildmorelines
Guest
Posts: n/a
 
      10-03-2004
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";
 
Reply With Quote
 
 
 
 
Andres Monroy-Hernandez
Guest
Posts: n/a
 
      10-03-2004
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
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      10-03-2004
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
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      10-03-2004
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
 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      10-03-2004
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?"
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      10-11-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help in a conditional html test Cogito HTML 4 09-18-2005 11:28 AM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Urgent Help Required from Gurus - Conditional databinding CGuy ASP .Net 4 10-01-2003 03:50 AM
Help writing conditional blocks estafford ASP .Net 2 08-19-2003 06:58 PM
Help: conditional attribute assignment itsme VHDL 1 07-23-2003 03:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57