News for hackers. Stuff that matters.

Python's if __name__=="__main__": main() in other languages

This behavior is very hard to Google. For now, I'm calling it "scripted main" and I'm cataloging it for several programming languages.

Ruby: if __FILE__ == $0 then main end

Perl: unless(caller) { main; }

...

https://github.com/mcandre/scriptedmain

Does anyone know the syntax for this in GNU Smalltalk?

( 3 comments | 13 points | on HN )
Re:Python's if __name__=="__main__": main() in other languages by damncabbage on Monday August 08, 2011 @08:33AM (#2859808)
PHP: if (realpath($argv[0]) == __FILE__) main();

(Presuming you've gone and defined the main() function somewhere. No module conventions like with Python here.)

Re:Python's if __name__=="__main__": main() in other languages by booz on Friday August 05, 2011 @03:24PM (#2851344)
for node.js

if (!module.parent) { // your main here }

Re:Python's if __name__=="__main__": main() in other languages by mcandre on Friday August 05, 2011 @03:39PM (#2851404)