7
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Would using SetDelayed help in your case? $\endgroup$ – Yves Klett Jan 10 '15 at 16:32
  • $\begingroup$ @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. $\endgroup$ – Murta Jan 10 '15 at 16:54
  • $\begingroup$ 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. $\endgroup$ – Mr.Wizard Jan 10 '15 at 19:34
  • $\begingroup$ @Mr.Wizard yes! Much better using ConnPackage, I forgot BeginPackage reads System and the loaded package. $\endgroup$ – Murta Jan 10 '15 at 19:45
  • $\begingroup$ @Mr.Wizard about your first question, with Kuba solution I don't need to put any extra code inside ConnPackage. $\endgroup$ – Murta Jan 10 '15 at 19:49
7
$\begingroup$

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.

Does it fit your needs?


Different approach.

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*)

SetAttributes[$connectionIP, Protected];

Needs["ConnPackage`"];
$\endgroup$
  • $\begingroup$ Very nice! I like both ways. Nice use of UpValues. Tks +1 $\endgroup$ – Murta Jan 10 '15 at 19:33
  • $\begingroup$ @Murta Great! I'm glad I can help :) $\endgroup$ – Kuba Jan 11 '15 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.