在我們訪問的屬性在我們訪問的屬性文件,如下 do "config.cfg";
如何使用input.properties像概念文件在TCL腳本
同樣的方式如何訪問性能perl腳本如下 <property file="input.properties"/>
文件Ant腳本文件在TCL腳本中。 任何人都可以幫我解答嗎? 在此先感謝...
在我們訪問的屬性在我們訪問的屬性文件,如下 do "config.cfg";
如何使用input.properties像概念文件在TCL腳本
同樣的方式如何訪問性能perl腳本如下 <property file="input.properties"/>
文件Ant腳本文件在TCL腳本中。 任何人都可以幫我解答嗎? 在此先感謝...
好的,如果你想讓它像Perl一樣愚蠢,只需在Tcl中使用source
這個文件即可。
配置文件樣本(名爲config.tcl
):
# Set "foo" variable:
set foo bar
要裝入此配置文件:-ing
source config.tcl
後source
,你可以訪問你的變量foo
在腳本中。
與Perl中,惡意用戶可能會把類似
exec rm -rf ~
在「配置文件」,並祝願大家好運。
最簡單的機制是使其成爲腳本或使其成爲數組的內容。以下是如何做後者同時還支持意見:
proc loadProperties {arrayName fileName} {
# Put array in context
upvar 1 $arrayName ary
# Load the file contents
set f [open $fileName]
set data [read $f]
close $f
# Magic RE substitution to remove comment lines
regsub -all -line {^\s*#.*$} $data {} data
# Flesh out the array from the (now clean) file contents
array set ary $data
}
然後你會使用這樣的:
loadProperties myProps ~/myapp.props
if {[info exists myProps(debug)] && $myProps(debug)} {
parray myProps
}
隨着你的home目錄(稱爲myapp.props
)的文件是這樣的:
# Turn on debug mode
debug true
# Set the foos and the bars
foo "abc"
bar "Harry's place downtown"
你可以做比這更復雜的事情,但它給你一個容易的格式。
如果你喜歡使用一個可執行的配置,只是做:
# Define an abstraction that we want users to use
proc setProperty {key value} {
# Store in a global associative array, but could be anything you want
set ::props($key) $value
}
source ~/myapp_config.tcl
如果你想限制操作,但又不會導致(多)的麻煩,你需要一個稍微複雜的方法:
interp create -safe parser
proc SetProp {key value} {
set ::props($key) $value
}
# Make a callback in the safe context to our main context property setter
interp alias parser setProperty {} SetProp
# Do the loading of the file. Note that this can't be invoked directly from
# within the safe context.
interp invokehidden parser source [file normalize ~/myapp_config.tcl]
# Get rid of the safe context; it's now surplus to requirements and contaminated
interp delete parser
安全性開銷很低。
皮爾斯
$var = "test";
的等效是Tcl的
set var "test"
所以,如果你想把它當作簡單的Perl中,我建議kostix answer。
但你也可以嘗試使用dicts作爲配置文件:
這看起來像
var {hello world}
other_var {Some data}
foo {bar baz}
我個人喜歡用這個,它甚至允許嵌套:
nestedvar {
subvar {value1}
subvar2 {value2}
}
和評論:一種哈哈CK,其實有鑰匙#
# {This is a comment}
解析:
set fd [open config.file]
set config [read $fd]
close $fd
dict unset config #; # Remove comments.
訪問:
puts [dict get $config var]
puts [dict get $config nestedvar subvar]
但如果你想真的像$var = "foo";
(有效期Perl代碼但不是Tcl),那麼你必須解析這個fil你自己。
一個例子:
proc parseConfig {file} {
set fd [open $file]
while {[gets $fd line] != -1} {
if {[regexp {^\s*\$([^\s\=]+)\s*\=\s*(.*);?$} $line -> var value]} {
# The expr parses funny stuff like 1 + 2, \001 inside strings etc.
# But this is NOT perl, so "foo" . "bar" will fail.
set ::$var [expr $value]
}
}
}
缺點:不允許多行設置,將拋出一個錯誤,如果存在一個無效的值,並允許命令注入(但你的Perl的解決方案確實太)。
你可以從屬性加載器設置全局變量,但這可能是一個壞主意。如果加載一個腳本,你可以考慮使用一個安全的解釋器,並嚴格規定哪些命令是暴露的。或者你可能不打擾;畢竟它是用戶的應用程序,如果他們想打破它,他們可以做到這一點。 :-) – 2013-04-08 10:23:01
我是Tcl的新手,但在屬性Gruop名稱和valsue訪問方面似乎很複雜。在perl中很容易。只需要在config.cfg文件中定義像'$ var =「test」;'我們可以在腳本中用'print $ var' '在Tcl中以相同的方式指定該值'我想用'name ='value 」。 – picnic4u 2013-04-08 11:20:32