Is it possible to read file locally inside procedure?

Viewed 86

If I do this

u:=1;
save u, "test.txt";
u:=2:

then

test1:=proc() local u;
  read "test.txt";
end proc:
test1():
'u'=u;
                      u = 1

So, when reading a file inside a procedure the variable u was assigned globally. Is it possible to make sure it stays local?

EDIT: The goal for procedure test1() is to do some stuff, which needs variables with values from a file. That file was made after some long lasting calculation, which was made in some other work. In order to avoid errors I need to make sure that test1() won't change the values of variables in my current work since names can be the same.

1 Answers

Your question lacks the context of what you're trying to accomplish with test1, and why you feel the need for a convoluted mechanism.

Ie, why don't you simply supply the assigned u as one of the arguments passed to test1?

Here are two alternatives, for which any subsequent call/invocation of test1 doesn't result in the global name u being reassigned. These both result in procedures where that value is actually part of the procedure body.

Here's the first alternative,

restart;
test1proto := proc() local u;
    u := __dummyu;
end proc:

u := 1:

test1 := subs(__dummyu = u,
              eval(test1proto)):
u:='u':

test1();

           1

'u'=u; # global u is still unassigned

         u = u

In that way you can "shoehorn" any value you want (including, say, a running value of u at the higher level) into test1 so that its local u attains that value. You can also repeat that subs call, to instantiate the template procedure again at other values.

Note that this mechanism can replace a dummy name in the template procedure without your necessarily having assigned to u. Ie,

restart;
test1proto := proc() local u;
    u := __dummyu;
end proc:

test1 := subs(__dummyu = 1,
              eval(test1proto));

     test1 := proc () local u; u := 1 end proc

test1();

           1

'u'=u; # global u was never unassigned

         u = u

You can make that kind of subs call and substitute whatever you want for the dummy name.

Here's another alternative, in which the source of the test1 procedure is also in its own text file. That source file for test1, which I've named "test1.mpl" looks like this, say,

test1 := proc() local u;
$include "u.mm"
end proc:

Now, in my Maple session,

restart;
u := 1:
save u, "u.mm";
u:='u':

read "test1.mpl"

print(eval(test1));

     proc () local u; u := 1 end proc

test1();

           1

'u'=u; # global u is still unassigned

          u = u

Here again you can save futher, different values for u, and then once again read the source file "test1.mpl" and get different procedures with different values.

Note that I still suspect that your actual goals (as yet unclear in full) might be accomplished with mechanisms simpler than any of this. But exactly how you intend on using test1, and any restrictions of your programmatic flow, are unknown.

Related