0
$\begingroup$

This question already has an answer here:

I open a new notebook and type

ClearAll[test];
test[x_] := x + 42;

I save it as Temp.nb and also as Temp.m, using the menu Save As... > Wolfram Mathematica Package (*.m). I now open up another notebook and type

<< "Temp.m"

which succeeds. But the symbol test is not defined.

?? test
Information::notfound: Symbol test not found. >>
test[3]
test[3]

I'm confused, because the documentation for Get, a.k.a., <<, says

<< name reads in a file, evaluating each expression in it and returning the last one.

It should have evaluated the expressions in Temp.m, then, I should think, and returned the [value of] the last one, which would be Null. One of those expressions is a SetDelayed for the symbol test and should have installed test in the Global namespace, no?

Fresh kernel session, fresh reboot of MMA, also fails if file is saved as Wolfram Language Package (*.wl), yada yada.

$\endgroup$

marked as duplicate by m_goldberg, Mr.Wizard Aug 18 '14 at 5:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Only initialization cells in .m files are evaluated when loaded. $\endgroup$ – m_goldberg Aug 18 '14 at 5:37
  • $\begingroup$ More accurately only Initialization Cells are saved as active (non-comment) lines in the package file. $\endgroup$ – Mr.Wizard Aug 18 '14 at 5:50

Browse other questions tagged or ask your own question.