Does such a thing exist?
Ruby:
if __FILE__ == $0
main
end
Perl:
unless(caller) {
main;
}
Lua:
if type(package.loaded[(...)]) ~= "userdata" then
main(arg)
else
module(..., package.seeall)
end
Exotic multiline shebangs and argv trickery do the job.
See 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.
].
main
function which is not run when this code is imported by other code. In other words, Python'sif __name__=="__main__"
.main
function from the other module/file, import the former and call the latter.