Operating System - HP-UX
1819794 Members
3431 Online
109607 Solutions
New Discussion юеВ

Perl problem: logical operators.

 

Perl problem: logical operators.


From a file, I read a list of files or directories.

I must check if in my current subtree , it exists. The individual match
( if it is a file or it is a directory) is successful but it associate
both of them with the OR operator ,it fails.

I mean : if ( -e $file ) -> success
if ( -d $file ) -> suc ess
if ( -e $file ) || ( -d $file ) -> fails
if ( ( -e $file) || ( -d $file ) ) -> fails

Can you help me with the correct synxtax?

Thanks,


Raul
3 REPLIES 3
H.Merijn Brand (procura
Honored Contributor

Re: Perl problem: logical operators.

if (-e $file || -d _) {
action
}

Enjoy, Have FUN! H.Merijn [ introducing the often forgotten shortcut and speedup _ for last used file in stat op ]
Enjoy, Have FUN! H.Merijn
Georg Tresselt
Honored Contributor

Re: Perl problem: logical operators.

In fact, if either ( -e $file ) or ( -d $file ) is TRUE, the total outcome shut be TRUE.

What if you use "or" instead of "||", i.e.

if ( -e $file ) or ( -d $file )

Also, does it help if you put some brackets around the condition, i.e.

if ( ( -e $file ) || ( -d $file ) )

http://www.tresselt.eu
Ralph Grothe
Honored Contributor

Re: Perl problem: logical operators.

procura's suggestion is the preferred way,
because usage of the thingy '_' in the second test avoids another stat() syscall.
The difference between || and 'or' is that the latter has lesser precedence which can make a decisive difference but not in this case, so there should be no difference

For details please consult

perldoc -f -x
perldoc perlop
Madness, thy name is system administration