[plt-scheme] Can a module tell if it's being run interactively?

From: Richard Cobbe (cobbe at ccs.neu.edu)
Date: Tue Apr 15 19:12:31 EDT 2008

Is there a way for code in a module to tell if it's being run in the
DrScheme REPL or as part of a script?

Right now, I'm trying to debug a script that reads input from stdin and
writes output to stdout.  I've got the file structured as follows:

    #lang mzscheme

    (define (run)
       (pretty-print (munge-input (read))))

    (define munge-input ...)


Now, If my script hits a bug, my first idea is to load the module into
DrScheme -- which immediately stops, waiting for input.  The input to the
script is larger than I want to type at the REPL, so I comment out the call
to `run', reload the module, then do (with-input-from-file ... run).  After
I get the bug fixed, I inevitably forget to uncomment the call to run, so
the script is a NOP the first time I run it.

It'd be really handy to say something like

    #lang scheme

    (define (run)
      (pretty-print (munge-input (read))))

    (define munge-input ...)

    (unless (running-at-repl?) (run))

where `running-at-repl?' is provided by the "scheme" language.  That way,
`run' gets called when I run the script from the Unix shell but not when I
load it into DrScheme and hit the "Run" button.

OCaml's Sys.interactive
is basically what I'm looking for here.  In OCaml, one can write something

    let _ =
      if not !Sys.interactive
      then run ()
      else ()

at module top-level to get the behavior I described above.

Is this possible?  If not, would it be possible to add this feature?



Posted on the users mailing list.