PERL Conditions
Placed in () when used with if, while, etc...
Any expression is considered true as
long as it does not evaluate to the null string ("") or 0 or "0".
Common:
http://www.perldoc.com/perl5.6/pod/perlop.html
Relational Operators
-
<
-
Returns true if the left argument is numerically less than the right argument.
-
>
-
Returns true if the left argument is numerically greater than the right argument.
-
<=
-
Returns true if the left argument is numerically less than or equal to the
right argument.
-
>=
-
Returns true if the left argument is numerically greater than or equal to
the right argument.
-
lt
-
Returns true if the left argument is stringwise less than the right argument.
-
gt
-
Returns true if the left argument is stringwise greater than the right argument.
-
le
-
Returns true if the left argument is stringwise less than or equal to the
right argument.
-
ge
-
Returns true if the left argument is stringwise greater than or equal to
the right argument.
Equality Operators
-
==
-
Returns true if the left argument is numerically equal to the right argument.
Compares the length when used with strings (use eq)
-
!=
-
Returns true if the left argument is numerically not equal to the right argument.
-
<=>
-
Returns -1, 0, or 1 depending on whether the left argument is numerically
less than, equal to, or greater than the right argument. Often used with
Sort.
-
eq
-
Returns true if the left argument is stringwise equal to the right argument.
-
ne
-
Returns true if the left argument is stringwise not equal to the right argument.
-
cmp
-
returns -1, 0, or 1 depending on whether the left argument is stringwise
less than, equal to, or greater than the right argument. Often used with
Sort
-
! or not
-
Logical Not. The not form has a lower precedent and is therefor "safer"
-
&& or and
-
Logical AND. Short circuit. The and form has a lower precendent and
is therefor "safer"
-
|| or or
-
Logical OR. The or form has a lower precendent and is therefor "safer".
Short circuit evaluation applies; as in: open(IN, "infile.txt") or die "can't
open"; where the die doesn't happen unless the open returns 0. (neat huh?)
Also $foo = $bar or $this or $that; will return the first of $bar, $this,
or $that which contain a value.
``lt'', ``le'', ``ge'', ``gt'' and ``cmp'' use the collation (sort) order
specified by the current locale if use locale is in effect. See the perllocale
manpage.
File-test operators
http://www.perldoc.com/perl5.6/pod/func/X.html
-
-e filename
-
Test for the existance of a file or directory
-
-r
-
File is readable
-
-w
-
File is writable
-
-x
-
File is executable
-
-o
-
File is owned by effective uid.
-
-z
-
File has zero size.
-
-s
-
File has nonzero size (returns size).
-
-f
-
File is a plain file.
-
-d
-
File is a directory.
-
-M
-
Modified. Age of file in days when script started. e.g. if (-M $src <=
-M $dest) {print "Backup already up to date";}
-
-A
-
Accessed. Same for access time.
-
-C
-
Created. Same for inode change time.
Example:
while (<>) {
chop;
next unless -f $_; # ignore specials
#...
}
Example:
print "Can do.\n" if -r $a || -w _ || -x _;
stat($filename);
print "Readable\n" if -r _;
print "Writable\n" if -w _;
print "Executable\n" if -x _;
See also:
Uppercase operators (e.g. EQ instead of eq) used to be ok, but they aren't anymore. They cause "bareword found where operator expected" errors as of perl version 5.
+
file: /Techref/language/perl/condition.htm, 4KB, , updated: 2009/12/29 15:47, local time: 2024/12/30 06:25,
|
| ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://massmind.org/Techref/language/perl/condition.htm"> Perl conditions</A> |
Did you find what you needed?
|
.