Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Python's if __name__=="__main__": main() in other languages
13 points by mcandre on Aug 5, 2011 | hide | past | favorite | 3 comments
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?



PHP: if (realpath($argv[0]) == __FILE__) main();

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


for node.js

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





Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: