Questions about this topic? Sign up to ask in the talk tab.

Perl/Basics/Boolean Logic/Operators

From NetSec
Revision as of 01:12, 19 July 2012 by Chantal21I (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Mathematical

  • =

The = operator assigns a value to a variable.

  • ==

The == operator is used to test if a variable is equal to a value or another variable.

  • !

The ! operator means "not". It applies to = and any variable. For example, "if (!$scalar)" is used to determine if the $scalar variable is null, zero, or undefined. When used before a = operator, this becomes "not equal to".

  • eq

Sometimes because variables may not be a string or integer, the eq operator is used to determine if the two are equal.

The following operators are used for greater than, less than, and greater than or equal to, less than or equal to, etc; similar to other languages:

  • >
  • gt
  • <
  • lt
  • >=
  • gte
  • <=
  • lte

Regular Expressions

The ~ operator is used with regular expressions, which are covered later in this article. The ~ operator can be used in a variety of ways:

  • =~
  • !~

Regular expressions can also be very useful when using perl as a shell glue language. As an example:

"find | perl -nwl -e "m:zs: and print" 

will pipe the output of the find command into perl, which will then apply the regular expression m:zs: which is an expression which only looks for those two characters in those orders. The use of "and" makes perl only apply the print operation to the line if the first match returns a "true." The use of -nwl means that newlines are stripped, warnings are enabled, and that the input which doesn't match isn't printed as well.