Don Porter wrote:
> In article <443e700e$1@usenet01.boi.hp.com>, Andrew Falanga wrote:
>>I'm working on a new source command that will manage a list of loaded
>>libraries for me. The code for the new source command is:
>
> Let us count the opportunities for improvement...
I never claimed to be the best :-)
>
>> rename source _source
>>
>> proc source { pathToFile } {
>> global libraryIndex
>> if { ![file exists $pathToFile] } {
>> Log "File: $pathToFile does not exist"
>> exit
>> }
>
> Process exit is a pretty severe reaction to a missing file.
> Sure that's what you want?
In this case, I'm pretty sure that is what I want. I'm typically going
to be sourcing in libraries and if I can't find them, the script won't
run anyway.
>
>> uplevel #0 [list _source] $pathToFile
>
> This should be:
>
> uplevel 1 [list _source $pathToFile]
>
> The revised [list] arguments are in order to support values
> of $pathToFile that contain spaces. That's the whole point of
> [list] quoting. Just [list _source] alone is essentially pointless.
Honestly, it seemed that way to me too. However, I was going off of a
thread "extend Tcl commands? no can do?" originally posted to this list
Oct. 3, 2000 by a p_lavarre. In this thread a Dan Kuchler advised the
OP that his redefinition of the source command should look like this:
proc ::source {args} {
puts test
return [uplevel [list source_orig] $args]
}
With the exception of the return addition, he had what I wrote. Also,
believe it or not, no one in that entire thread corrected Dan as you've
corrected me here. Therefore, although it seemed to be rather
meaningless, I wrote it that way.
>
> The change in the "level" argument makes sure that your replacement
> [source] command sources in the same context as the original one
> would. With [uplevel #0] you're forcing it into the global context,
> and some uses of [source] will not be happy with that as you discovered.
Yes, indeed. I just tried it with the change you've given. Thank you.
It works now.
>
>> regsub -all {/} $pathToFile " " pathToFile
>>
>> lappend libraryIndex [lindex $pathToFile end]
>
> Wouldn't it be easier to do:
>
> lappend libraryIndex [file tail $pathToFile]
At this one, I'm wishing I'd read the file manual page before assuming
that file didn't do this. This is actually the solution I was looking
for, but didn't think there was anything in TCL that did it. I feel stupid.
>
>> }
>
--
---------------------------------------------
Andrew R. Falanga (a non-HP employee)
Hewlett-Packard Company
11311 Chinden Blvd.
Boise, Idaho
---------------------------------------------
Please note: The e-mail address is purposely
mangled. I do not wish my account at HP to
become a spam haven.
Received on Sun Apr 30 03:10:37 2006