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

    #!/path/to/mzscheme
    #lang mzscheme

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

    (define munge-input ...)

    (run)

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

    #!/path/to/mzscheme
    #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
(http://caml.inria.fr/pub/docs/manual-ocaml/libref/Sys.html#VALinteractive)
is basically what I'm looking for here.  In OCaml, one can write something
like

    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?

Thanks,

Richard


Posted on the users mailing list.