SPUG: flip-flops

Colin Meyer cmeyer at helvella.org
Wed Feb 6 22:45:56 CST 2002


Hi Yitzchak,

On Wed, Feb 06, 2002 at 04:36:51PM -0800, Yitzchak Scott-Thoennes wrote:
> In article <20020206162754.N1565 at hobart.helvella.org>,
> Colin Meyer <cmeyer at helvella.org> wrote:
> >You can test the state of scalar .. .  That's what you typically do
> >with it.
> >
> >  if ($bar .. $baz) {
> >    print "flip-flop's state is true.\n";
> >    print "Next evaluation will check the right hand term.\n";
> >  }
> >  else {
> >    print "flip-flop's state is false.\n";
> >    print "Next evaluation will check the left hand term.\n";
> 
> >  }
> 
> That works with ... but not ..;  I think you need to say
>   if (($bar .. $baz) =~ /^\d+$/) {
> to find out what the state is at the time of the print.

You are partially correct. My example wouldn't be accurate for ... 
either!

I knew something was wrong with my code. Who put that unmaintainable ..
operator in there, anyway? ;-)

This example will hopefully shed some light:

  #!/usr/local/bin/perl
  
  print "\n",' 'x22,"..\t\t...\n\n";
  
  for (<DATA>) {
    chomp;
    printf '%-22s',$_;

    $dd  = /start/ ..  /stop/;
    $ddd = /start/ ... /stop/;

    print "[$dd]\t\t[$ddd]\n";

    printf '%22s','next term to test:  ';
    # clearly maintainable code follows ;-)
    print $dd&&$dd!~/E/?'right':'left',"\t\t",
          $ddd&&$ddd!~/E/?'right':'left',"\n\n";
  }
  
  __DATA__
  1  outside
  2  start
  3  inside
  4  stop
  5  outside
  6  start inside stop
  7  outside or in?
  8  start
  9  inside
  10 stop

Ok, so when you need to test for the edge condition with either .. or ...,
then the code isn't quite so intuitive.  If I saw something like:

  if (($bar .. $baz) =~ /^\d+$/) { # watch that precedence
    print "flip's true and not about to change\n";
  }

then I'd have to go back and check the docs. Well, at least the
first time. ;-)

Have fun,
-C.

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org





More information about the spug-list mailing list