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.
$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;
$perens = ((($a + $b) * c) / $d);
$a++; # $a now 1
print "$a\n";
$a--; # $a now 0;
print "$a\n";
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 = ();
my $a = 0;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;
$b = "cadabra";
print $a.$b; # prints abracadabra
$c = $a x 3; # abraabraabra
print "-" x 80;
$a = "abra";Note, 'x' has a slightly different function when in list mode
@ones = (5) x @ones; #initialize all elements in @ones to 5's
@ones = (1) x 80; # a list of 80 1's
($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
($a && $b); # true if $a AND $b are both true. Short circuits if $a falseNote, 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.
$a += $b; # $a is now $a + $b;
$a .= $b; # is now $a concatenated to $b as a string
$a = $b; # $a is set to the value of $b, changing $b, does not change $a
$array[3] = 5;
$hash{$key} = "something";But also in some strange, unexpected, and exciting ways:
substr($a, 3, length($b)) = $b; # replaces characters 4-length($b) in $a with $b
substr($a, 3, 0) = $b; #inserts the contents of $b into $a at the 4th character. Note, $a is now permenantly changed.
($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
($a == $b); # numeric equality
(-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
(-e $file); # true if $file exists
Page Information
|
Wiki Information |
Recent PBwiki Blog Posts |