From: Peng Yu on
I'm puzzled why I can not use the first line below. Would you please
let me know how write it such that the if clause is at the beginning
rather than at the end.

if(defined $opt_o) print "$opt_o\n";

print "$opt_o\n" if defined $opt_o;

Regards,
Peng
From: sisyphus on
On May 21, 8:16 am, Peng Yu <pengyu...(a)gmail.com> wrote:
> I'm puzzled why I can not use the first line below. Would you please
> let me know how write it such that the if clause is at the beginning
> rather than at the end.
>
> if(defined $opt_o) print "$opt_o\n";
>

if(defined $opt_o) { print "$opt_o\n" }

Cheers,
Rob
From: Dr.Ruud on
Peng Yu wrote:

> Would you please
> let me know how write it such that the if clause is at the beginning
> rather than at the end.
>
> if(defined $opt_o) print "$opt_o\n";

defined $opt_o and print $opt_o, $/;

--
Ruud
From: Tad McClellan on
Peng Yu <pengyu.ut(a)gmail.com> wrote:

> I'm puzzled why I can not use the first line below.


Did you look up the "if" statement in Perl's docs?

perldoc perlsyn

Compound Statements
...
a block is delimited by curly brackets, also known as braces.
We will call this syntactic construct a BLOCK
...
if (EXPR) BLOCK


> Would you please
> let me know how write it such that the if clause is at the beginning
> rather than at the end.
>
> if(defined $opt_o) print "$opt_o\n";


that is

if (EXPR) EXPR

but you need

if (EXPR) BLOCK


if (defined $opt_o) {print "$opt_o\n"}


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.