Search code examples
haskellenvironment-variablescgihugs

fetch environment variables in a cgi with haskell/hugs without a package


I am writing a cgi-script in Haskell. I am restricted to use only hugs/runhugs.

#!/opt/local/bin/runhugs

module Main where

main = do
        putStrLn ("content-type: text/plain\n")
        putStrLn ("Hello, Server!")

So far so good. But now I want to get the server's environment variables. For example the "SCRIPT_NAME" environment variable.

With bash I can do:

#!/bin/bash

echo "content-type: text/plain;"
echo ""

echo $SCRIPT_NAME

With the result: /path/to/script.cgi in the browser-window.

For Haskell I found something alike: script <- getEnv "SCRIPT_NAME", but

#!/opt/local/bin/runhugs

module Main where

main = do
        putStrLn ("content-type: text/plain\n")
        scriptname <- getEnv "SCRIPT_NAME"
        putStrLn scriptname

doesn't work.

Is it possible to do it in a way sort of like that ?

  1. plain without an import, or
  2. with an import possible in hugs

Solution

  • Try import System.Environment (getEnv).