Now that you can get data in and out of your perl program, and even store the data in scalars, arrays, and hashes, what can you do with it?

Perl is a complete programming language, offering a diverse cornucopia of operators to massage your data.

  • Arithmetic: Perl supports all the usual means of manipulating numerical data. Those of you with experience in C, C++, or Java will recognize its syntax. Also, remember, 'Please Excuse My Dear Aunt Sally'

    $perens = ((($a + $b) * c) / $d);

    $exponent = $a ** $b;

    $product = $a * $b;

    $quotient = $a / $b;

    $remainder = $a % $b;

    $sum = $a + $b;

    $diff = $a - $b;

    $bitwise_left = $a << $b;

    $bitwise_right = $a >> $b;

    $bitwise_and = $a & $b;

    $bitwise_or = $a | $b;

    $bitwise_xor = $a ^ $b;

  • autoincrement/autodecrement: extremely useful operators which increase, decrease an integer value by one.

    my $a = 0;

    $a++; # $a now 1

    print "$a\n";

    $a--; # $a now 0;

    print "$a\n";

    Note, the change to the integer actually happens after the full line is processed, e.g. its as if $a is unchanged until the semi-colon or block is encountered:

    my $a = 0;

    print $a++."\n"; #prints 0 then increments

    print $a."\n"; # prints 1

    $a = 0;

    if ($a++ < 1) {

      # tests $a < 1, which will be true, then increments

    }

    print "$a\n"; # prints 1

    my %count = ();

  • string: In addition to the arithmetic operators (which do work on strings by converting them to integers), Perl provides a number of string operators.

    $a = "abra";

    $b = "cadabra";

    print $a.$b; # prints abracadabra

    $c = $a x 3; # abraabraabra

    print "-" x 80;

    Note, 'x' has a slightly different function when in list mode

    @ones = (1) x 80; # a list of 80 1's

    @ones = (5) x @ones; #initialize all elements in @ones to 5's

  • logical: Perl provides many 'short-circuit' operators which allow conditional tests without excessive nesting (see below for more information about flow control and truth).

    ($a && $b); # true if $a AND $b are both true. Short circuits if $a false

    ($a and $b); # true if $a and $b true, short circuits if $a false

    ($a || $b); # true if $a OR $b is true

    ($a or $b); # true if $a or $b is true

    Note, there are differences in the way these operators will behave with other operators (including assignment operators), based on Perl's rules of precedence. Most of the time they are interchangeable.

  • assignment: Perl provides many ways to assign the results of an operation to a scalar (remember where scalars can be stored). All of the above operators can be used in assignment style to modify the contents of a scalar.

    $a = $b; # $a is set to the value of $b, changing $b, does not change $a

    $a += $b; # $a is now $a + $b;

    $a .= $b; # is now $a concatenated to $b as a string

  • L-Values: In many cases, the things that Perl allows you to put on the right side of an assignment can also be placed on the left side of an assignment (e.g. they can be assigned to). This is true for many expected things:

    $hash{$key} = "something";

    $array[3] = 5;

    But also in some strange, unexpected, and exciting ways:

    substr($a, 3, 0) = $b; #inserts the contents of $b into $a at the 4th character. Note, $a is now permenantly changed.

    substr($a, 3, length($b)) = $b; # replaces characters 4-length($b) in $a with $b

  • comparison: Perl has separate comparison operators for numbers and string:

    ($a == $b); # numeric equality

    ($a != $b); #numeric inequality

    ($a < $b); # numeric less-than

    ($a > $b); # numeric greater-than

    ($a <= $b); # numeric less-than-or-equal-to

    ($a >= $b); # numeric greater-than-or-equal-to

    ($a <=> $b); # The space-ship operator returns 0 if numeric equal, 1 if $a > $b, -1 if $a < $b (very useful in sort)

    ($a eq $b); # string equality

    ($a ne $b); # string inequality

    ($a gt $b); # asci string greater-than

    ($a lt $b); # asci string less-than

    ($a cmp $b); # string space-ship operator

  • file tests: Perl provides a number of file test operators to use in truth expressions. Here are a few:

    (-e $file); # true if $file exists

    (-r $file); # true if $file exists and is readable

    (-w $file); # true if $file exists and is writable

    (-d $dir); # true if $dir exists and is a directory

    (-f $file); # true if $file exists and is a regular file (not a directory, or other special unix 'file')

    (-T $file); # true if $file exists and is a text file

 


Page Information

  • 4 months ago [history]
  • View page source
  • You're not logged in
  • No tags yet learn more

Wiki Information

Recent PBwiki Blog Posts