Re: source'ing files in multiple places - why bad?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: source'ing files in multiple places - why bad?

From: Donal K. Fellows <donal.k.fellows@manchester.ac.uk>
Date: Mon Feb 27 2006 - 01:59:16 CET

gamename wrote:
> Its mostly a question of working with lots of legacy scripts. We have
> thousands of scripts laying about. Some of these date back over 10
> years. Often people will reuse parts of old scripts and simply copy
> the "source" section blindly. People don't realize that those files
> being sourced will generally source others. Before long, there will be
> files that have been sourced many times in the same script invocation.

There are a whole bunch of issues to consider. I'm too tired to think of
them all ;-) but here's a version of [source] that tries hard to cache
the code, at least getting rid of the hit of going back to the
filesystem every time round. Note that it assumes that [pwd] never
changes (or that you use absolute paths to [source]) and that you never
alter any [source]d file during the life of the program.

   proc source filename {
      global source_cache
      if {![info exist source_cache($filename)]} {
         set f [open $filename]
         set source_cache($filename) [read $f]
         close $f
      }
      info script $filename
      uplevel 1 [list if 1 $source_cache($filename)]
   }

Donal.
Received on Sun Apr 30 02:16:26 2006