这样的事情存在吗?
红宝石:
if __FILE__ == $0
main
end
的Perl:
unless(caller) {
main;
}
的Lua:
if type(package.loaded[(...)]) ~= "userdata" then
main(arg)
else
module(..., package.seeall)
end
答案 0 :(得分:1)
充满异国情调的多线shebangs和argv技巧可以完成这项任务。
请参阅Rosetta Code。
scriptedmain.st:
"exec" "gst" "-f" "$0" "$0" "$@"
"exit"
Object subclass: ScriptedMain [
ScriptedMain class >> meaningOfLife [ ^42 ]
]
| main |
main := [
Transcript show: 'Main: The meaning of life is ', ((ScriptedMain meaningOfLife) printString); cr.
].
(((Smalltalk getArgc) > 0) and: [ ((Smalltalk getArgv: 1) endsWith: 'scriptedmain.st') ]) ifTrue: [
main value.
].