Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

djberg96 (2603)

djberg96
  (email not shown publicly)

Journal of djberg96 (2603)

Thursday October 28, 2004
11:49 PM

Forgive me Father, for I have used "goto"

[ #21581 ]
Well, it took 7 years, but I finally found a use for "goto" outside of BASIC. In my particular case, it was within a Ruby C extension. I wanted to create 'tail -f' functionality for monitoring the Windows event log. It makes sense when you take blocks into consideration. To wit:

e = EventLog.open("Application")

e.tail{ |rec|
   puts "Record added to log"
   p rec
}

The tail method sits in an infinite loop (which is accomplished with the 'goto'), yielding a (custom) struct every time a record is written to the event log.

On a stranger note, the Windows function NotifyChangeEventLog() can only detect a maximum of one change every five seconds. That means, even if 20 records were written to the event log in under 5 seconds, that block would only pick up the first one. No, there's no way around it.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • You aren't showing the code where you felt it necessary to use a goto, so I cannot judge its validity. I suspect that it isn't needed though.

    For the record, I know of exactly two necessary uses of goto in Perl.

    • static VALUE eventlog_tail(VALUE self){
         ELS* ptr;
         VALUE rbBlock, rbSource, rbServer;
         HANDLE hEvent;
         DWORD dwWaitResult;
         int rv;

         Data_Get_Struct(self,ELS,ptr);

         eventblock:
         hEvent = CreateEvent(
            NULL,    // default security attributes
            FALSE,   // no manual reset
            FALSE,   // create as not signaled
            NULL

      • First of all that is written in C, not Ruby. There are a number of situations where you need a goto in C that you wouldn't in Perl or Ruby because of the availability of named loop control.

        Secondly I don't see why you aren't using while to get the infinite loop rather than goto.

        Thirdly you can save some lines by not always using braces. For instance:

              if(rb_block_given_p())
                 rbBlock = rb_block_proc();
              else
               

        • First of all that is written in C, not Ruby.

          I said is was a C extension for Ruby. I showed you the frontend. The C code above is the backend.

          Secondly I don't see why you aren't using while to get the infinite loop rather than goto.

          I suppose I could have done while(1) or something. It just didn't feel right.

          Thirdly you can save some lines by not always using braces.

          I hate that, sorry.

          • 1, 3. Fair points.

            About the second. I'd suggest re-reading Go To Statement Considered Harmful [acm.org]. The fact that your indentation and control structures are in conflict makes your program's flow of control harder to understand upon first reading. Even if you aesthetically prefer a goto to an infinite while loop, I'd suggest that you indent from the label to the goto so that that section jumps out at the maintainer.