Variable Scope of Files Included Within Functions

From: Deane Barker (deane_at_slingandrock.com)
Date: 10/29/04


Date: 28 Oct 2004 19:51:28 -0700

I have a function that selects a file to include, then includes is.
The file is including within the function, like so:

  function include_file($file_name)
  {
    [lots of logic here to figure out the path...]
    require $file_name;
    return;
  }

My problem is that the file gets included *within* the function, so
all the code in the file inherits the function's scope. Any variables
declared in the include file are not global -- they're local to that
function.

Is there any way around this? Is there anything I can put in the
included file or in the function to make sure that variables declared
within it are global? (I can't just reference the variables before
the require, because I have no way of knowing what's in the include
file.)

Deane



Relevant Pages

  • Re: vbs output to txt file
    ... the reference count to that object will be decremented. ... As does going out of scope:) ... You are religiously setting objects to nothing and not ... and so am seriously lacking in both scripting expertise ...
    (microsoft.public.scripting.wsh)
  • Re: Lock-free reference counting
    ... The scope of t is the entire function foo, ... The reference's lifetime is considerably shorter than ... The only way for it to collect it is if no reference is ... Bindings are introduced by various language constructs. ...
    (comp.programming)
  • Re: Lock-free reference counting
    ... scope and value lifetimes are completely separate concepts. ... reference is dropped, then the object could be immediately destroyed ... the mere act of destroying ... so scope-based reference counting keeps values alive a lot longer ...
    (comp.programming)
  • Re: Article of interest: Python pros/cons for the enterprise
    ... create a less flexible Python feature that achieves the ... you can create a temporary object whose reference count will become ... especially in a complex multi-statement scope. ... If you can then request that arbitrary actions be taken automatically when those events happen, you can pair up resource acquisitions and releases very easily. ...
    (comp.lang.python)
  • Re: Scoping in VB.Net
    ... You may be slightly confused about scope, ... As an example of what I mean, think about memory leaks using a language ... variables are not value types (but reference types) and are declared on ... The Windows OS has a reference to it via a "Handle". ...
    (microsoft.public.dotnet.languages.vb)