help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Puzzled about scope/existence of globals


From: John W. Eaton
Subject: Re: Puzzled about scope/existence of globals
Date: Sat, 22 Nov 2003 22:17:14 -0600

On 22-Nov-2003, Tomer Altman <address@hidden> wrote:

| You know what? I was starting to notice some of the same strangeness,
| but I became distracted & never fully investigated it. Poking around,
| I've noticed some oddities:

I think you also said you were working with 2.1.40, which is old, but
you would only see one difference with 2.1.51 which is that global
values are always intialized to [] unless you specifically initialize
them yourself.  This changes the behavior of your second function.

| function scope_fun
| 
| if ( ! exist("x","var") )
|   global x = "foo"
|   x
| else
|   global x = "bar"
|   x
| endif
| 
| endfunction
| 
| octave> scope_fun
| x = foo
| octave> x
| error: `x' undefined near line 5 column 1
| octave> global x
| octave> x
| x = foo
| octave> x = 5
| x = 5
| octave> scope_fun
| x = 5
| octave> clear x
| octave> x
| error: `x' undefined near line 10 column 1
| octave> scope_fun
| x = 5
| octave> x
| error: `x' undefined near line 12 column 1
| octave> 
| 
| ---
| 
| My off-the-cuff analysis is that there is, in fact, somehow, *TWO*
| global scope name-spaces: one for the interpreter environment, and one
| for the "compiled" M-file environment. What I think is happening, is
| that as soon as one is updated, it will try to "sync" with the other
| environment. Of one of those global scopes' variables is 'cleared',
| then the other one persists. Also, it seems that the interpreter
| environment is "dominant", when they "sync"...

I'm not sure why you are guessing about the behavior.

| Here's the same test, but from the opposite perspective:
| 
| ---
| 
| function scope_fun2 
| 
|   global x;
| 
|   x
| 
|   clear x;
| 
|   x
| 
| endfunction
| 
| octave> scope_fun2
| error: `x' undefined near line 5 column 3
| error: called from `scope_fun2' in file
| `/home/taltman/taltman-judean/code/octave/M-files/scope_fun2.m'

With 2.1.51, you would see

  ans = [](0x0)

for line 5, then the undefined variable error on line 9 after the
clear statement.

| octave> global x
| octave> x = 5
| x = 5
| octave> scope_fun2
| x = 5
| error: `x' undefined near line 9 column 3
| error: called from `scope_fun2' in file
| `/home/taltman/taltman-judean/code/octave/M-files/scope_fun2.m'
| octave> x
| x = 5
| octave> 
| 
| ---
| 
| So I guess this bizarreness is due to how 'clear' operates, as well.

I don't think it is bizarre, so perhaps you can point out specifically
where you think it is bizarre, and the behavior you expect, and why.

Think of each global variable behaving as though it exists in its own
named Fortran COMMON block.  Clearing a global is like deleting that
COMMON block from the scope in which the clear statement appears.  The
globals associated with a function are created the first time a global
statement inside a function is evaluated, but do not disappear when
the function exits (that way, they are persistent across a function
call, even if they are only mentioned inside one function).

AFAIK, this is all Matlab-compatible behavior (with the exception of
allowing initialization in the global statement as an extension -- to
get that in Matlab, you have to do some extra work).  If you find some
way that global variables in Octave's do not work the same as they do
in Matlab, then please describe precisely how Octave's current (i.e.,
2.1.51 or later) behavior is different.  If you simply don't like the
way that globals work in Matlab, then I think you should be asking
questions about that over in comp.soft-sys.matlab.

jwe



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]