[tpm] Stupid question time!

arocker at vex.net arocker at vex.net
Sun Feb 7 08:27:21 PST 2010


I realise that this is a bit late, but if you run this program (also
attached), and study the results, it might help clarify the effects of
different precedences.

#! /usr/bin/perl

# not_vs_! illustrates the difference in effect caused by operator precedence

use strict;
use warnings;

explore (' if  (not $foo  &&  $bar ne "")  {1;} ',
         ' if ((not $foo) && ($bar ne "")) {1;} ');

explore (' if  (! $foo  &&  $bar ne "")  {1;} ',
         ' if ((! $foo) && ($bar ne "")) {1;} ');

sub explore {

    foreach my $expr (@_) {
        print "\nExpression $expr\n";
        print "foo bar !foo  bnn result \n";
        foreach my $foo (0, 1) {
            my $nfu     = (not $foo);
            foreach my  $bar ("", 1) {
                my $bnn     = ($bar ne "");
                my $result  = eval "$expr";
                printf "%2s%4s%5s%5s%4s\n",$foo, $bar, $nfu, $bnn, $result ;
            }
        }
    }
}
# not_vs_! Ends
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not_vs_!
Type: application/octet-stream
Size: 746 bytes
Desc: not available
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20100207/e3d2769a/attachment.obj>


More information about the toronto-pm mailing list