I'm using ReadList to pull the declarations off of a package like so:

PackagePullDeclarationsAction // Clear
PackagePullDeclarationsAction[
Hold[
_Begin | _BeginPackage |
CompoundExpression[_Begin | _BeginPackage, ___]
]
] :=
Throw[Begin];
PackagePullDeclarationsAction[e : Except[Hold[Expression]]] :=
Sow@e;

Reap[
Catch@
PackagePullDeclarationsAction@Hold[Expression]
]
][[2, 1]]


The PackagePullDeclarationsAction either uses Sow to sow an expression or Throw to abort the ReadList call. And this works as intended:

$pkgFile = URLDownload[ "https://raw.githubusercontent.com/b3m2a1/mathematica-BTools/\ master/Packages/AppBuilder.m", FileNameJoin@{$TemporaryDirectory, "AppBuilder.m"}
];

Begin["test"];
Take[
PackagePullHeader[$pkgFile] // DeleteCases[Hold[Null]], 5 ] End[]; {Hold[$AppDirectoryRoot::usage =
"The directory root for finding apps";],
Hold[$AppDirectoryName::usage = "The basic extension to a directory for locating apps";], Hold[$AppDirectory::usage = "Joins the root and name";],
Hold[AppPath::usage = "A path parser for a given app name";],
Hold[AppDirectory::usage =
"Used by AppPath find appropriate directories";]}


The problem is that it also loaded all of my "Private" symbols into the primary Context:

Names["test*"]~Take~5

{"testa", "testadd", "testapp", "testAppAddContent", \
"testAppAddDocPage"}


What's going on here? ReadList clearly didn't ignore the Throw so did it load everything first then apply PackagePullDeclarationsAction?

Is there a good work-around for this? Am I just missing something trivial?

• @Kuba I am indeed. I don't want anything to evaluate. I just want the stuff "above" the Begin. Hence the context issues. My old version used a loop until it hit that. I wanted something faster. – b3m2a1 Aug 18 '17 at 7:03
• Ok, I see the point now. – Kuba Aug 18 '17 at 7:04
• Looks like you will have to stick to a loop as ReadList reads all first. – Kuba Aug 18 '17 at 7:31
• @Kuba that was my guess. It's too bad that type of idiom (abort when pattern is found) isn't supported. I also tried a version that loads it all like this but in a hidden context and moves the necessary symbols to the appropriate context via Context[s]=... but that was slower than the loop. – b3m2a1 Aug 18 '17 at 7:32

Simple order of operations.

ReadList[expr, f["Expression"]]


constructs the list {f[e1], f[e2], ...} for each expression in expr. Then that will evaluate, or not, depending on what f is. However, every expression in expr has already been created. And since the BeginPackage never evaluated, all symbols without a known context will end up in the current context.

This is a general feature of reading package files. You'd encounter a similar issue with Import["foo.m", "HeldExpressions"]. Because the context can change dynamically, each expression must be read and evaluated sequentially to guarantee the expression. Your only alternative would be to write packages which fully qualify every single symbol in them so there's never a context issue.

• I just reverted to an old loop-base method. It's actually faster than loading in a private context and exposing via Context[...]=.... Any chance a sequentially evaluating ReadList gets supported ever? (basically one that can be aborted midway through with a Throw) – b3m2a1 Aug 18 '17 at 21:08

Maybe you can do something like:

Begin["test"];
Block[{BeginPackage=Identity,Begin=Throw},
Catch@Get[package]
];
End[]


$pkgFile = URLDownload[ "https://raw.githubusercontent.com/b3m2a1/mathematica-BTools/master/Packages/AppBuilder.m", FileNameJoin@{$TemporaryDirectory, "AppBuilder.m"}
];

Begin["test"];
Block[{BeginPackage=Identity, Begin=Throw},
Catch @ Get[\$pkgFile]
];
End[];

#->MessageName[#, "usage"]& /@ Symbol /@ Names["test*"][[;;5]]


{testAppAddContent -> "Adds a file to the app", testAppAddDocPage -> "Adds a doc page for a symbol to the app", testAppAddGuidePage -> "Adds a guide to the app", testAppAddPackage -> "Adds a package", testAppAddPalette -> "Adds a palette to the app"}

• that's a clever idea. Autoloading symbols would kill it, of course (if they called a Begin` as mine tend to), but that might not be an issue. I may use just that for Version 2.0 of the pre-processor. – b3m2a1 Aug 19 '17 at 9:14