Autrijus Tang wrote: > jn Tue, Feb 22, 2005 at 10:54:26AM -0000, Rafael Garcia-Suarez wrote: > > I had this silly idea, to try to add %ENV to pugs. That would be an > > interesting exercise. So I can write CGI scripts. Maybe. But I don't > > know what's the Right way to do it in Perl 6. Any hints ? > > In Perl6 I think you do it by populating the %*ENV hash, when the > script is first run. To add it, hack Main.hs line 102 to add a new > SGlobal symbol of a VHash. If you already have a list of pairs "x", > it is as simple as: > > VHash $ MkHash (listToFM x)
Here's a patch to add this. It's heavily cargo-culted, I need to read some docs about haskell some day. As I wasn't able to get FiniteMaps to work I implemented my own kludgy function to flatten pairs for the hash. Also I suspect that the type of the internal thingy in %*ENV might not be right, so you may want to turn it in a VHash instead (I haven't found any difference). Comes with tests. Note that this %*ENV is not magical. It should be sufficient to write simple http-GET CGI scripts, though. Index: t/op/magic.t =================================================================== --- t/op/magic.t (revision 0) +++ t/op/magic.t (revision 0) @@ -0,0 +1,13 @@ +# Tests for magic variables + +use v6; + +say "1..2"; + +# Tests for %*ENV + +# it must not be empty at startup +if (0 + %*ENV.keys > 0) { say "ok 1"; } else { say "not ok 1"; } + +# PATH is usually defined. But this test is not portable +if %*ENV{"PATH"} ne "" { say "ok 2"; } else { say "not ok 2"; } Index: MANIFEST =================================================================== --- MANIFEST (revision 170) +++ MANIFEST (working copy) @@ -71,6 +71,7 @@ t/op/hash.t t/op/inc.t t/op/join.t +t/op/magic.t t/op/pair.t t/op/pop.t t/op/push.t Index: src/Main.hs =================================================================== --- src/Main.hs (revision 170) +++ src/Main.hs (working copy) @@ -22,6 +22,7 @@ import Parser import Help import Pretty +import Posix main :: IO () main = do @@ -92,14 +93,20 @@ runFile file = do withArgs [file] main +flattenPairs :: [(x,x)] -> [x] +flattenPairs [] = [] +flattenPairs ((a,b):q) = [a,b] ++ flattenPairs q + runProgramWith :: (Env -> Env) -> (Val -> IO ()) -> VStr -> [VStr] -> String -> IO () runProgramWith fenv f name args prog = do + environ <- getEnvironment env <- emptyEnv [ Symbol SGlobal "@*ARGS" (Val $ VList $ map VStr args) , Symbol SGlobal "@*INC" (Val $ VList []) , Symbol SGlobal "$*PROGNAME" (Val $ VStr name) -- , Symbol SGlobal "$*STDIN" (Val $ VStr str) , Symbol SGlobal "$*END" (Val VUndef) + , Symbol SGlobal "%*ENV" (Val $ VList $ map VStr $ flattenPairs environ) ] -- str <- return "" -- getContents let env' = runRule (fenv env) id ruleProgram prog Index: src/Posix.hs =================================================================== --- src/Posix.hs (revision 170) +++ src/Posix.hs (working copy) @@ -21,9 +21,11 @@ rename, removeLink, sleep, + getEnvironment, ) where #ifdef PUGS_HAVE_POSIX +import System.Posix.Env import System.Posix.Files import System.Posix.Process import System.Posix.Unistd @@ -47,4 +49,7 @@ sleep :: Int -> IO () sleep _ = fail "'sleep' not implemented on this platform." +getEnvironment :: IO [(String, String)] +getEnvironment = [] + #endif