这样的事情存在吗?

ruby :

if __FILE__ == $0
    main
end

Perl:
unless(caller) {
    main;
}

卢阿:
if type(package.loaded[(...)]) ~= "userdata" then
    main(arg)
else
    module(..., package.seeall)
end

最佳答案

异国情调的多行shebang和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.
].

关于smalltalk - Python是否是GNU Smalltalk的__name __ = ="__main__"惯用语?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6960994/

10-14 19:07