MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Needs[], BeginPackage[], filename

  • To: mathgroup at
  • Subject: [mg101065] Re: [mg101038] Needs[], BeginPackage[], filename
  • From: Leonid Shifrin <lshifr at>
  • Date: Tue, 23 Jun 2009 07:04:49 -0400 (EDT)
  • References: <>

Hi Scot,

May be I miss some sources where alternatives are shown, but your
suggested workaround is exactly what Roman Maeder also suggests as a
possible solution to this very problem in his "Programming in Mathematica"
3-rd edition (p. 50)

So, unless things changed significantly in the package system since 1997,
this is probably it. You can, of course, add your own definition to
BeginPackage, which will itself execute Needs and then call the standard
BeginPackage, Something like this:

With[{filepattern = (Rule | RuleDelayed)[_String, _String]},
  BeginPackage[context_String, dependson : {(_String | filepattern) ..}] /;
    MemberQ[dependson, filepattern] :=
   With[{filesAndContexts = Cases[dependson, filepattern]},
    Needs[#2, #1] & @@@ filesAndContexts;
     dependson /. (Rule | RuleDelayed)[file_String, cont_String] :> cont]];

This adds a new syntax that allows you to intersperse the context names with
rules that tell BeginPackage which file names to associate with which

Example of use (insert your own context names):

   "AttributesOfPureFunctions.m" :> "AttributesOfPureFunctions`",
   "NbToHTML`", "CheckOptions.m" -> "CheckOptions`"}];


But, a disclaimer :) - I can not guarantee that this will always work
I tried a few simple cases and it seems to work for me, but I hope I won't
be flamed for this :)

Hope this helps.


On Mon, Jun 22, 2009 at 1:21 AM, Scot T. Martin <smartin at>wrote:

> Needs[] allows the context and filename to be specified.
> BeginPackage[] allows a context and not a file name.
> Then, my packages require two steps, as in the following example:
> ** start **
> Needs["sizedistribution`", "size functions.m"]
> BeginPackage["tandemdma`", {"sizedistribution`"}]
> ** stop **
> Are two steps really necessary or am I missing something very simple?
> My goal would be to just forego Needs[] and handle everything in one step
> with BeginPackage, but I cannot figure out a way to feed the filename to
> BeginPackage[].
> I had a search on the MathArchives, but I did not succed in locating any
> material addressing this question.
> Thanks for any quick hints!

  • Prev by Date: Re: Two questions about modification of a matrix.
  • Next by Date: Re: from the list select
  • Previous by thread: Re: Needs[], BeginPackage[], filename
  • Next by thread: Re: Needs[], BeginPackage[], filename