Judi
Judi

Reputation: 157

Unable to parse environment variable in another module

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.

Upvotes: 0

Views: 277

Answers (1)

arrowd
arrowd

Reputation: 34401

getEnv returns IO String, why do you call read on its result? You can use it right away.

If you really want this, execute show "asd" and look what string should look like to be correctly parsed by read.

Upvotes: 5

Related Questions