SPUG: flip-flops

Colin Meyer cmeyer at helvella.org
Wed Feb 6 14:00:34 CST 2002


Hi Jason,

On Wed, Feb 06, 2002 at 04:15:10AM -0800, dancerboy wrote:

[...]

> 
> OTOH, my complaint may be that, if I understand the docs correctly 
> (and again, I'm not sure that I do), it appears that the .. and ... 
> operators implicitly maintain state information from previous uses of 
> the operator.  I consider that to be a Bad Thing:  the result of an 
> operator should be completely determined by its *current* operands, 
> regardless of any previous uses of the operator.  

Am I to understand that you'd avoid the /g modifier switch to the
matching operators? /g certainly causes the matching operators (actually
their operands) to maintain state.

Consider:

  while ($s =~ s/(\w+)/g) {
    print "Found some word characters: $1\n";
  }

> Any sort of state information should be made explicit (such as storing
> it in a variable, e.g. $in_foo in your example above, or encapsulating
> it in an object).

By this thought, the above snippet might be rendered:

  my $still_looking=1; # here's the state information
  my $s_copy = $s;    
  while ($still_looking) {
    if ($s_copy =~ s/.*?(\w+)//) {
      print "Found some word characters: $1\n";
    }
    else {
      $still_looking=0;
    }
  }

> By utilizing implicit state changes, you are in effect 
> hiding important parts of the logic of your code, thus making it 
> harder to read. ("Hiding" here should be understood in the bad sense 
> of "obscuring", not in the good sense of "encapsulating".  There's a 
> difference, albeit a subtle one.)

By taking advantage of operators that can maintain state, one can
clear their code of extra distractions.

btw, How do you consider encapsulating state information in an object
different than encapsulating state information in an operator?

  my $matcher = REGEX::withState->new(qr/(\w+)/);
  while ($match = $matcher->get_next_match($s)) {
    print "Found some word characters: $match\n";
  }

To be honest, in the regex case, the state is maintained in the string,
rather than the regex operator.  See perldoc -f pos.  So, the string
is an object. ;-) But operators can be objects too.  See Ch 5 in Damian's 
book.  

> 
> Again, just because a language has certain features, doesn't mean 
> that you ought to use them. (And again, I'm talking about code that 
> other people may have to read.  If scalar .. is clear to you, and 
> you're the only one maintaining the code, hey, more power to you...)

I'd certainly agree. You won't see me messing with $[, composing hash
keys like $h{good;one}, or any other of a number of nasty leftovers in
the language.  I even avoid the perfectly reasonable m??. 

However, I will continue to use handy operators, like scalar .., the
hook operator (trinary ?::), and m//g. 


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