Help: About 'if' structure

A

Amy Lee

Hello,

I'm a Perl newbie, and I have a problem about the 'if' structure.

If I wanna use more than one cpmparisons, how can I accomplish this
function in perl?

For example, in Shell script:

if [ "$#" -eq 0 -a -e "$somefile" ]

'-a' means "AND".

And in perl, I have two comparison, I wanna combine them.

1.if (@ARGV == 0)
2.if (-e $somefile)

Thank you in advance~

Amy Lee
 
T

Tad McClellan

If I wanna use more than one cpmparisons, how can I accomplish this
function in perl?

For example, in Shell script:
'-a' means "AND".


In Perl, "and" means "AND", and "&&" means "AND" too.

They are documented in the perlop.pod man page.
 
P

Paul Lalli

I'm a Perl newbie,
Greetings.

and I have a problem about the 'if' structure.

Not really. You have a question about logical operators. You just
want to use them as the condition to an if statement.
If I wanna use more than one cpmparisons, how can I accomplish this
function in perl?

For example, in Shell script:

if [ "$#" -eq 0 -a -e "$somefile" ]

'-a' means "AND".

And in perl, I have two comparison, I wanna combine them.

1.if (@ARGV == 0)
2.if (-e $somefile)

There are two "AND" operators in Perl. '&&' and 'and'. They are
functionally equivalent, but '&&' has a higher precedence than 'and'.
So in your case, you might do:

if (@ARGV == 0 and -e $somefile) { ... }
or
if (@ARGV == 0 && -e $somefile) { ... }

For a full list of operators in Perl, and their precedence chart, open
a command window and type:
perldoc perlop

Paul Lalli
 
T

Tad McClellan

Amy Lee said:
And Furthermore, if I wanna use "OR", how to accomplish it in perl?


"or" or "||"

You should check the documentation for the software you use *before*
asking hundreds of people around the world to read it to you.
 
P

Paul Lalli

And Furthermore, if I wanna use "OR", how to accomplish it in perl?

..... did you think Tad and I pointed you at the `perldoc perlop`
documentation for the heck of it?

You've run out of free fish. Time to learn how to fish.

Paul Lalli
 
M

Michele Dondi

And Furthermore, if I wanna use "OR", how to accomplish it in perl?

Given that and and && were the solution to your "AND" question, it
surprisingly turns out that or and || are those to the current one.
Also surprisingly, this is clearly explained in the same document you
have already been pointed to, which is

perldoc perlop

and which I encourage you to read in its entirety. It's an awesome
reading, I guarantee!


Michele
 
M

Michele Dondi

You've run out of free fish. Time to learn how to fish.

She'll tell you she's too hungry to lose her time playing games with
lines and hooks! ;-)


Michele
 
T

Tim Southerwood

Tad McClellan coughed up some electrons that declared:
In Perl, "and" means "AND", and "&&" means "AND" too.

Although the operator precedence (priority) is different for

"&&" vs "and"

and

"||" vs "or"

Worth watching out for that, and not very obvious.

To be fair, "perldoc" is a skill in itself - took me a while to discover its
potential, back in the early days.

To the OP:

perldoc perl

will list the "books" available to perldoc

perldoc perlfunc

is very useful

perldoc -f somefunctionname

is a quick lookup for the definition of a core function

and

perldoc Some::Module

documents a module ("man Some::Module" often works too, at least on all
Linux systems I've used)

I think you would benefit from getting a couple of the O'Reilly books too -
if you get only one, get the "Perl Cookbook" IMHO - instant recipes for all
sorts of interesting problems, and a good demo of use of the languages in
solving a variety of problems. Then again, I learn best by example, so you
may like to consider "Learning Perl". It's a pretty good introduction.

HTH

Tim
 
P

Paul Lalli

perldoc perl

will list the "books" available to perldoc

No it won't. It will give you an overview of the language, with
select other documents available. `perldoc perltoc`, on the otherhand,
will give you the Table of Contents of perldoc.

Paul Lalli
 
T

Tim Southerwood

Paul Lalli coughed up some electrons that declared:
No it won't. It will give you an overview of the language, with
select other documents available. `perldoc perltoc`, on the otherhand,
will give you the Table of Contents of perldoc.

Paul Lalli

You're right of course - I should have said "some of the books" - for some
reason I tend to jump to perldoc perl for no apparently good reason, apart
from (probably bad) habit.

I'll get me coat...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top