I have some packages that uses database connections inside it, let's call it ConnPackage. The connection IP is a public package variable, and has a default value:

$connectionIP="127.0.0.1".  This variable is used to read database values when the package is loading. So change it value after package load don't solve the problem. The problem is similar to pass args in shell wolfram script, but must be done in the notebook. The question is: How could I read the ConnPackage, using Needs[], but setting $connectionIP to be another value?

UPDATE

I can solve the problem defining a variable in System. But I don't feel it's a good pattern. Here is my attempt. Inside ConnPackage I can do:

$connectionIP=If[StringQ[#],#,"127.0.0.1"]&[System$argConnectionIP]


So, if there is no value in System$argConnectionIP, the standard IP will be load, otherwise it will use System$argConnectionIP.

• Would using SetDelayed help in your case? – Yves Klett Jan 10 '15 at 16:32
• @YvesKlett SetDelayed don't help in this case, as the package use $connectionIP as it runs to load other definitions. @OleksandrR. I made an update with how I solved it, but I don't fell it's a good way. – Murta Jan 10 '15 at 16:54 • I saw you ping in Chat. I'm afraid I don't really understand the question at the moment. First, what is wrong with your own solution? Second, why do you have to use System ? Cannot you use ConnPackage$connectionIP too? Perhaps I don't actually understand what you wish to accomplish; if that's the case pardon me. – Mr.Wizard Jan 10 '15 at 19:34
• @Mr.Wizard yes! Much better using ConnPackage, I forgot BeginPackage reads System and the loaded package. – Murta Jan 10 '15 at 19:45
• @Mr.Wizard about your first question, with Kuba solution I don't need to put any extra code inside ConnPackage. – Murta Jan 10 '15 at 19:49

You can catch next assignment with TagSetDelayed:

$connectionIP /: Set[$connectionIP, _] := (ClearAll[$connectionIP];$connectionIP = "whatever");
Needs["ConnPackage"]


You should make this TagSetDelayed statement in proper context so use Begin or provide explicit context.

If you know what is done inside the package, and you know that $connectionIP is a constant you can just do: $connectionIP = "whatever";                (*keep in mind the context*)
Needs["ConnPackage"];

• Very nice! I like both ways. Nice use of UpValues`. Tks +1 – Murta Jan 10 '15 at 19:33