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: Tomer Altman
Subject: Re: Puzzled about scope/existence of globals
Date: Sat, 22 Nov 2003 19:15:40 -0800 (PST)

Hi Glenn,

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:

---

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"...

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'
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.

Lol, I guess Octave has "closures" after all. :-)

JWE, we could use your guru expertise. :-)

~Tomer



On Nov 22, 2003 at 5:26pm, Glenn Golden wrote:

gdg >Date: Sat, 22 Nov 2003 17:26:02 -0700
gdg >From: Glenn Golden <address@hidden>
gdg >To: address@hidden
gdg >Subject: Puzzled about scope/existence of globals
gdg >Resent-Date: Sat, 22 Nov 2003 18:26:10 -0600
gdg >Resent-From: address@hidden
gdg >
gdg >
gdg >Given the function foo(), defined as
gdg >
gdg >    function foo()
gdg >   global g;
gdg >   exist("g", "var")
gdg >    endfunction
gdg >
gdg >I found the following to be surprising:
gdg >
gdg >    1>   foo
gdg >    ans = 0                        # ok
gdg >
gdg >    2>   global g
gdg >
gdg >    3>   g.barf = 1;
gdg >
gdg >    4>   foo
gdg >    ans = 1                        # ok
gdg >
gdg >    5>   clear g 
gdg >
gdg >    6>   whos g    
gdg >                           # ok
gdg >    7>   foo
gdg >    ans = 1                        # huh?
gdg >
gdg >Perhaps this is expected behavior, but I don't understand it.
gdg >Can someone explain the rules regarding scope and existence
gdg >of globals in a circumstance like this?
gdg >
gdg >Thanks,
gdg >
gdg >- Glenn
gdg >
gdg >
gdg >
gdg >-------------------------------------------------------------
gdg >Octave is freely available under the terms of the GNU GPL.
gdg >
gdg >Octave's home on the web:  http://www.octave.org
gdg >How to fund new projects:  http://www.octave.org/funding.html
gdg >Subscription information:  http://www.octave.org/archive.html
gdg >-------------------------------------------------------------
gdg >
gdg >



-------------------------------------------------------------
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]