I am developing a bioinformatics sequence analysis application which
contains about 20 modules. One of those modules, the 'Par' module,
encapsulates a record of about 30 configuration parameters needed
throughout the whole application. The parameter record is mostly
static but some values can be overwritten by command-line options. Is
there a way to make the (possibly modified) parameters record globally
accessable throughout all modules? This would be much more convenient
than having to pass the parameters record to virtually all functions
in my application. THX for helping.
Andreas
_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
For my application I've a module where each runtime option is a ref and
then I use the Arg module to set the proper value. Don't know if there
are better way of doing this...
:)
p
--
++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
++
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
See http://www.fsf.org/philosophy/no-word-attachments.html
d.
2006/7/19, David MENTRE <david....@gmail.com>:
> Hello,
>
> 2006/7/19, Andreas Biegert <andreas...@googlemail.com>:
> > Is
> > there a way to make the (possibly modified) parameters record globally
> > accessable throughout all modules?
>
> Define a global variable in the Par module, use it in the other modules.
>
> ==par.ml==
> type param = { mutable a: int; }
>
> let params = { a = 3; }
> ==
>
> ==foo.ml==
> let bar = Par.params.a
>
> let _ = Par.params.a <- bar + 1
> ==
>
> > This would be much more convenient
> > than having to pass the parameters record to virtually all functions
> > in my application.
>
> <personal taste>I find this purely functional approach much cleaner
> because you know where your global variables are read or modified
> directly by looking at function parameters.</personal taste>
>
> Best wishes,
> d.
This is a bit ugly, but we use it in our Adwords API toolkit:
-------------------------------------------------- stdargs.mli
val username : string
val password : string
val client : string option
val token : string
val update : bool
val verbose : bool
val args : string list
-------------------------------------------------- stdargs.ml
let username = ref ""
let password = ref ""
let client = ref ""
let token = ref ""
let update = ref false
let verbose = ref false
let args = ref []
(* followed by some code which tries to read default values
* from $HOME/.adwordsapidata -- code omitted
*)
(* parse the command line parameters *)
let argspec = [
"--username", Arg.Set_string username, "Adwords account username.";
"--password", Arg.Set_string password, "Adwords account password.";
"--client", Arg.Set_string client, "Adwords account client (optional).";
"--token", Arg.Set_string token, "Adwords account token.";
"--update", Arg.Set update, "Perform updates.";
"--verbose", Arg.Set verbose, "Be verbose.";
]
let anon_fn str = args := str :: !args
let usage =
Sys.executable_name ^ " [--options]"
let () = Arg.parse argspec anon_fn usage
let username = !username
let password = !password
let client = if !client = "" then None else Some !client
let token = !token
let update = !update
let verbose = !verbose
let args = List.rev !args
---
Then the code just stuff like:
open Stdargs
if verbose then printf "this is verbose mode\n"
Rich.
--
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com
> On Wed, Jul 19, 2006 at 01:11:22PM +0200, Andreas Biegert wrote:
>> I am developing a bioinformatics sequence analysis application which
>> contains about 20 modules. One of those modules, the 'Par' module,
>> encapsulates a record of about 30 configuration parameters needed
>> throughout the whole application. The parameter record is mostly
>> static but some values can be overwritten by command-line options. Is
>> there a way to make the (possibly modified) parameters record globally
>> accessable throughout all modules? This would be much more convenient
>> than having to pass the parameters record to virtually all functions
>> in my application. THX for helping.
>
> This is a bit ugly, but we use it in our Adwords API toolkit:
>
> -------------------------------------------------- stdargs.mli
> val username : string
You certainly mean
val username: string ref
> val password : string
> val client : string option
> val token : string
> val update : bool
> val verbose : bool
> val args : string list
>
> -------------------------------------------------- stdargs.ml
> let username = ref ""
> let password = ref ""
> let client = ref ""
> let token = ref ""
> let update = ref false
> let verbose = ref false
> let args = ref []
>
[ ... ]
>
> Then the code just stuff like:
>
> open Stdargs
>
> if verbose then printf "this is verbose mode\n"
You certainly mean
if !verbose then printf "this is verbose mode\n"
>
> Rich.
--
pad
No, I mean:
val username : string
Rich.
--
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com
_______________________________________________
> You certainly mean
>
> if !verbose then printf "this is verbose mode\n"
No. After he calls Arg parse, he does:
let username = !username
let password = !password
let client = if !client = "" then None else Some !client
let token = !token
let update = !update
let verbose = !verbose
let args = List.rev !args
and loses the refs.
William D. Neumann
---
"There's just so many extra children, we could just feed the
children to these tigers. We don't need them, we're not doing
anything with them.
Tigers are noble and sleek; children are loud and messy."
-- Neko Case
Life is unfair. Kill yourself or get over it.
-- Black Box Recorder
and I confirm :-)
I use the same trick of dereferencing the references used for
command-line parsing as soon as it is done and I find this very
convenient. This can be realized in the very first module on your
ocaml link command, and thus in whole the remaining of your code you
do not need to use the deref operator (!) anymore.
--
Jean-Christophe
For a couple of other approaches see
http://caml.inria.fr/pub/ml-archives/caml-list/2004/12/a0924032de03d517cb8cb8f2adde6c94.en.html
Jeff