SPUG: flip-flops

Michael R. Wolf MichaelRunningWolf at att.net
Sun Feb 10 13:47:36 CST 2002

dancerboy <dancerboy at strangelight.com> writes:


> Perhaps my complaint should more properly be with the
> docs, rather than with the operator itself.  To be
> perfectly honest, I read and reread that section of the
> docs, and eventually just gave up; I *still* don't fully
> understand what .. and ... do in scalar context. And I
> don't think that I'm particularly dense when it comes to
> things like this -- if I had difficulty understanding it,
> I'm pretty sure most other developers will have difficulty
> with it as well.  It's possible that, if the docs
> contained a more coherent explanation of what these things
> do, I might change my opinion.  But given the current
> state of the docs, I'd say scalar .. is going to be
> difficult for the average Perl developer to decipher.

I, too, had a hard time with that documentation.  It sat on
the back burner for a few years before it became *very*
useful.  Then, as Colin pointed out, the alternative code
introduced a stuctural variable and a complex control
structure that was better coded with the flip-flop control
structure than the equivalent long idiomatic phrase.

Again, as you suggest, a good example would overcome bad
documentation and prevent even worse code.  Here's an
example that helped me figure it out.  Consider an RFC 822
email.  A header is from the first line (1) to the first
blank line (/^$/).  A body line is from that line until the
last line ($).  Note that dollar ($) means "end of string"
in the RE and "last line" in the flip-flop.

    $header .= $_ if 1 .. /^$/;
    $body   .= $_ if /^$/ .. $;

I've finally figured out the difference between .. and ...,
but don't have a good example.  The bad (contrived) example
I have is below.  It shows that the flip-flop could turn off
on the same line that turned it on.

#! /usr/bin/perl -w
while(<DATA>) {
    next if /^$/;		# Skip blank lines.
    next if m/^\s*/;		# Skip shell-style comments.
    $data .= $_ if /BEGIN/ ... /END/

# This is a multi-line data block.
    Long data goes here.
    It can span multiple lines.
    The beginning and ending tags are on separate lines.

# This is a single-line data block.
BEGIN Short data can start/end on same line.  END


Michael R. Wolf
    All mammals learn by playing!
        MichaelRunningWolf at att.net

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     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