Frage

I have written a script in haskell named testscript with the following code:

#!/usr/bin/env runhaskell

main = putStrLn "hello"

After making the script executable, I can run it using ./testscript. However, when I try to load the script with ghci (ie with :l testscript), I get the error

target `testscript' is not a module name or a source file

If I rename testscript to testscript.hs, and try loading with ghci again, I get the error

testscript.hs:1:0:  error: invalid preprocessing directive #!
phase `C pre-processor' failed (exitcode = 1)

If I remove the shebang line it works fine. However it is tedious to have to add a .hs extension to the script, remove the top line, then remove the .hs extension and add the shebang line every time I want to try the script in ghci (which is pretty common everytime I want to make a change to it). Is there an easier way to do this?

I'm using ghc version 7.0.3 under Mac OS X 10.6.8

War es hilfreich?

Lösung

You can use the -x option to tell GHCi (or GHC for that matter) to treat all following files as if they had the specified extension.

There doesn't seem to be any way to specify this option from within GHCi (for use with :load), but a workaround you can use if you want this is to create a symlink with a .hs extension and load that.

Your second problem with the shebang line is caused by the C preprocessor being run on your source file for some reason (my old GHC install on Ubuntu does not do this). You can disable this by using the -XNoCPP option.

So for your case, this should work from the command line:

ghci -x hs -XNoCPP testscript
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top