NB: These pages were mostly written in 2001 or so. The résumé dates are accurate but the code is aged and unlike whiskey, 8 year-old code doesn't usually taste better. For a look at my current skills and to see my CPAN modules, sample code, and code discussions, please see these pages instead: Perl resources and sample code and PangyreSoft.
Operator, Operator
Social links
View Ashley Pond V's profile on LinkedIn
Miscellaneous

Other pages

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 $count++; which will raise the count by one after the variable is interpolated. Roughly the same as $count = $count + 1; which is the same as $count += 1; but $count++; is so much more useful, compact, and idiomatic, and, alas, confusing.

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 perldoc perlop.

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 perldoc perlop for a bit more detail on what’s going on.

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 /<CODEBLOCK>/;
    $code_switch ^= /<CODEBLOCK>/; # toggle on/off on <CODEBLOCK>
    $code .= $_ if $code_switch and not /<CODEBLOCK>/;
}
print "-"x70;
print "\nCODE:\n$code\n";

__DATA__
Dear Mizz Rand, I just finished your book, "The Pumpkinhead." Great, I must say. The idea of going against the establishment by becoming the purist root of the establishment is a coup for fascists worldwide. I mean this in the most complimentary fashion. Women make terrible fascists traditionally. And you do write like a man. I'll bet you even vote and drink whiskey too. I can give no higher praise. Yours truly, W.S. Burroughs, A.G.A. <CODEBLOCK> # THIS IS SPACE FOR CODE that could be gathered and executed in an # eval statement. It's not really code here, obviously. This is just # for demonstration. <CODEBLOCK> Dear Mister Burroughs: Your letter was at once insulting and compelling so I reply. My citizenship, alas, prevents me from responding to your offer of friendship. I am no longer European. I am as American as a drunken Navajo in Gallup, New Mexico. It's terribly hot this summer, don't you agree? Perhaps you could send me a letter about Germans in hot weather. In any event, please do send more gorgeous filth. This shall further a character study I am working on for my next novel. Your favorite writer, Ayn Rand

Discussion

Give it a try.

Search these pages via Google
Text, original code, fonts, and graphics ©1990-2009 Ashley Pond V.