in reply to Control Flow Puzzle

I tend to break these down to a naked block:
{ $_ = <INFO>; return %Header unless defined $_ and !/^\037/; last if /^\* Menu:/; redo; }
And as I'm writing this, I'm wondering if something belongs between the last and redo lines there. What are you doing for non 37, non menu lines? Just skipping them?

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
RE: Control Flow Puzzle
by Dominus (Parson) on Nov 16, 2000 at 02:34 UTC
    Randal said:
    { $_ = <INFO>; return %Header unless defined $_ and !/^\037/; last if /^\* Menu:/; redo; }
    That's really interesting. Why do you prefer that to this:
    while (1) { $_ = <INFO>; return %Header unless defined $_ and !/^\037/; last if /^\* Menu:/; }
    This seems the same to me, only less abnormal.

    > What are you doing for non 37, non menu lines? Just skipping them?
    Yes, exactly so. The 37 line marks the end of the 'node'. If the function sees a 'menu' line before the 37 line, it knows that the node contains a menu, and the following code processes the menu up to the following 37 line. Lines that look different are not of interest to this function.

      Why the naked block instead of the infinite loop? Style thing, I guess. Over time, I usually end up hacking the naked block, adding and deleting conditions of restart and exit. The naked block has cleaner semantics for me, going up only when and where I tell it, and falling out otherwise. Certainly for this case I could have switched to an infinite loop once I saw that the form fits, but I don't know that when I first start, and I consider a naked block to be a more primitive form. {grin}

      -- Randal L. Schwartz, Perl hacker

      What about a loop-less solution?

      undef $/; $_=<>; s/(\n\037.*?\n\* Menu.*?\n\037)//; $mymenu=$1;
      This way you put all input into one string, and just pick out the menus one by one. No tricky loops anymore. I don't know how much you rely on the markers to start at the beginning of the line, but you could remove the newlines in the regex, of course.

      I pray I didn't say something very stupid here. Please excuse me if this is the case..... And credits to little, who only yesterday showed me the undef $/ trick.

      Cheers,

      Jeroen

      Post Posting: Mr. Schwartz, `Learning perl' was my first contact with perl, and I really loved it, and enjoyed reading it very much! So, thanks a lot...

      I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)