[Date Index]
[Thread Index]
[Author Index]
Re: Using Get in a Module
*To*: mathgroup at smc.vnet.net
*Subject*: [mg64221] Re: Using Get in a Module
*From*: albert <awnl at arcor.de>
*Date*: Tue, 7 Feb 2006 03:35:47 -0500 (EST)
*References*: <ds6voh$8nr$1@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
Hi Stu,
> Next, I made a function and added it to a package. The function now
> contained the << operator:
> BeginPackage["apackage`"];
> fun::usage = ...
> Begin["Private`"];
> fun[args]:=Module[{},(...;<<(fn);...)];
> End...EndPackage...
Note that there is a slight difference between
Begin["Private`"]
(* $Context will be "Private`" now, probably incidentaly shared with
"bpackage`" *)
and
Begin["`Private`"] (* $Context will be "apackage`Private`" now *)
where the latter is what you would want to use in most cases...
> The package notebook compiled with no errors, but when I did a Needs on
> the
> resulting .m file, I got a syntax error on the <<. (I notice that the
> error
> message still gives false line numbers.) I made this go away by replacing
> << with Get. This looks like a bug but, again, I can live with it.
> However the module does not seem to be able to access the symbols defined
> by the
> assignment statements. After the Get is processed, the new symbols appear
> in Global context of the top-level notebook, but are not visible to the
> package function that contained the Get.
This is because at the time you evaluate the function fun, the context is
Global` (which you can check by looking at $Context), so every new symbol
is created in Global`, no matter which context the function fun was
originally defined in.
> I played a bit with defining
> local symbols with the same name and by using Global`x in the package
> function,
> but none of this helped.
I think you should be able to get the code doing what you want with using
something like Global`x, but would not recommend to do it that way: Be
aware that if you do something like:
BeginPackage["somethingelse`"]
Needs["apackage"];
fun[args]
EndPackage[];
then the symbols will be created in context somethingelse`, not Global` and
break your code again. So it is no good idea to work with explicit
references to the Global` context.
> It appears that Mathematica has preprocessed the
> module enough that it can't cope with dynamic changes to the namespace.
In fact the contrary is true: Mathematica does nothing special when
evaluating the code within that module and that is probably what causes
your problems. Only at the time when the function fun is defined,
Mathematica puts all new symbols into the private package-context. You can
look at the outcome with ??fun. If you want the symbols in the file(s) fn
to appear in context apackage` , you must explicitly say so by switching
the context, eg:
fun[args]:=Module[{...},
...;
BeginPackage["apackage`"];
Get[fn];
EndPackage[];
...;
];
If you want them to appear in the private context of the package, you will
need an appropriate Begin[] and End[], too:
fun[args]:=Module[{...},
...;
BeginPackage["apackage`"];
Begin["`Private`"];
Get[fn];
End[];
EndPackage[];
...;
];
An alternative is of course to include these Statements within the file(s)
fn...
> Is this a feature or a bug? Is there a workaround?
I would say neither nor, everything works like docuented, I guess, also I'm
not sure where and how well this is documented. Call it a workaround or
whatever, the above should work in most circumstances, but of course I only
roughly know what you really want to achieve....
hth,
Albert
Prev by Date:
**Solve problems**
Next by Date:
**Re: Digital Image Processing: showing graylevel images in**
Previous by thread:
**Re: Using Get in a Module**
Next by thread:
**Re: Using Get in a Module**
| |