Unable to parse environment variable in another module

时间:2016-04-04 06:01:21

标签: haskell

I have 2 modules and each of them I read environment variables. In the main module it's being read correctly and in another one not:

--Main module

import Module2
startApp :: IO ()
startApp = do
  port <- liftM read $ getEnv "PORT" -- ok
  print $ "Listening the port " ++ (show port) ++ "..." -- ok
  run port app


-- it's not called immediately
myFMain = do
  Module2.myF 
  --.........



--Module2
----...........

myF :: IO MyData
myF = do
  env <- liftM read $ getEnv "ENV" :: IO String
  print $ "ENV is " ++ (show env) -- error Prelude.read: no parse
  --.........

I run it as PORT=3344 ENV=development stack exec my-app-exe. Might it be because it's not in the main module and it's thus loaded when I first call it? It's called when a user requests a url.

1 个答案:

答案 0 :(得分:5)

getEnv会返回IO String,为什么要在结果上调用read?你可以马上使用它。

如果你真的想要这个,请执行show "asd"并查看read正确解析的字符串应该是什么样的。