|
Operator, Operator
|
Social links
Class::Prototype
WWW::Spyder Javascript tricks serial() join function Smart quotes Text to Excel Developing Featherweight Web Services with JavaScript
Miscellaneous
|
|
| Operator, Operator
|
Description
An extremely convenient feature of Perl is the ability to assign value
to a variable that is part of its own assignment equation. Most coders
are familiar with autoincrements, like
Still, addition was easy for me to understand but there is a host of
other self-modifying operators. Modulo, exponents, exclusive or’ing,
other bit operators… they were not sinking in. Demonstrations are
the best teacher so I wrote a script to allow me to run the operators
all at once on a pair of values. That was how I learned to stop
worrying and love the Code #!/usr/bin/perl -w
# script name "operator-operator"
use strict; # and if that doesn't work, use belt
# we want two & only two numeric args
@ARGV == 2 and
2 == grep /^\d+$/, @ARGV
or die "Give me two numeric arguments.\n";
my $literal_one = '$one';
my @ops = qw( **= += *= &= <<= &&= -= /= |= >>= ||= .= %= ^= x= );
for my $op ( @ops ) {
my ( $one, $two ) = @ARGV;
print
" \$x = $one;\n",
" \$y = $two;\n",
" \$x $op \$y; # ($one $op $two;)\n";
my $copy_one = $one;
eval $literal_one .' '. $op . $two;
print " \$x becomes $one\n";
print ' ', '-'x22, "\n" unless $op eq $ops[-1];
} Usage jinx[116]>operator-operator 1 8 $x = 1;
$y = 8;
$x **= $y; # (1 **= 8;)
$x becomes 1
---------------------------------
$x = 1;
$y = 8;
$x += $y; # (1 += 8;)
$x becomes 9
---------------------------------
$x = 1;
$y = 8;
$x *= $y; # (1 *= 8;)
$x becomes 8
---------------------------------
$x = 1;
$y = 8;
$x &= $y; # (1 &= 8;)
$x becomes 0
---------------------------------
$x = 1;
$y = 8;
$x <<= $y; # (1 <<= 8;)
$x becomes 256
---------------------------------
$x = 1;
$y = 8;
$x &&= $y; # (1 &&= 8;)
$x becomes 8
---------------------------------
$x = 1;
$y = 8;
$x -= $y; # (1 -= 8;)
$x becomes -7
---------------------------------
$x = 1;
$y = 8;
$x /= $y; # (1 /= 8;)
$x becomes 0.125
---------------------------------
$x = 1;
$y = 8;
$x |= $y; # (1 |= 8;)
$x becomes 9
---------------------------------
$x = 1;
$y = 8;
$x >>= $y; # (1 >>= 8;)
$x becomes 0
---------------------------------
$x = 1;
$y = 8;
$x ||= $y; # (1 ||= 8;)
$x becomes 1
---------------------------------
$x = 1;
$y = 8;
$x .= $y; # (1 .= 8;)
$x becomes 18
---------------------------------
$x = 1;
$y = 8;
$x %= $y; # (1 %= 8;)
$x becomes 1
---------------------------------
$x = 1;
$y = 8;
$x ^= $y; # (1 ^= 8;)
$x becomes 9
---------------------------------
$x = 1;
$y = 8;
$x x= $y; # (1 x= 8;)
$x becomes 11111111
Instead of me droning on about it, why not try installing the script
on your own computer? You can run it with different arguments to see
how the operators are working and read the That’s how I learned to use exclusive or assignment to switch a simplistic embedded code parser on and off in just a few lines. Code #!/usr/bin/perl -w
use strict;
my $code_switch = 0; # start with the switch off
my $code;
while (<DATA>) {
print unless $code_switch or /<code>/i;
$code_switch ^= /<code>/i; # toggle on/off on <CODEBLOCK>
$code .= $_ if $code_switch and not /<code>/i;
}
print "-"x70;
print "\n$code\n";
__DATA__
|
|
|
Perl Books ·
CPAN ·
mod_perl ·
Perl Monks ·
Perl Mongers ·
Perl Journal ·
Use Perl ·
Perl Jobs ·
ActiveState ·
perldoc.perl.org ·
O’Reilly Perl ·
W3Schools tutorials ·
Ovid's CGI Course ·
Catalyst ·
Perl at Wikipedia
Text, original code, fonts, and graphics ©1990-2008 Ashley Pond V. |
||