9

Do these following idioms (to write a module which is also an executable/runnable) have a name of design pattern?

In Python, we can write a module as an executable too with if name == 'main': idiom:

if __name__ == "__main__":
    main()

Similar idiom can be found in Ruby:

if __FILE__ == $0
  main()
end

Also same effect can be achieved differently in Perl too:

main() unless caller;

In Tcl, you may write:

if {![info level] && [info script] eq $::argv0} {
    main
}

Although these are implemented in different ways, they share the same goal: make single script file both a module and an executable/runnable. It seems to me a design pattern. How do you call them? I personally have been called them as Executable Module or Runnable Module, but I want to know the more common name if it exists.

hkoba
  • 91
  • 2
  • 2
    As a side note, none of these languages' communities really goes in for the "design patterns" idea. Really, design patterns are repeatable workarounds for features that are missing in a language, and it's usually better to just have those features in the language. But that doesn't mean there aren't names for the languages' idioms, of course. – abarnert Jul 04 '18 at 03:52
  • 1
    At least in Python and Tcl, I've seen the term "main guard" (or something "`__main__` guard" in Python, for obvious reasons) used for the more general, lower-level feature of guarding code from being run on import, at least in Python and Tcl. But that term applies whether you're using the idiom to write a source file that works both as a runnable script and as an importable module, or to guard against `multiprocessing` code running in child processes, or anything else. And I don't know of a cross-language name for the general idea of source-that-works-as-script-or-module (however implemented). – abarnert Jul 04 '18 at 03:53
  • 1
    As for your own terms, the problem with "executable module" and "runnable module" is that, at least in Python, they both seem to imply a module that's runnable via `-m` more than one that's a script-or-module. (Plus, I've seen "runnable module" used to describe a module that has "entry point" functions that get auto-generated scripts at `pip install` time, which is yet another different thing.) – abarnert Jul 04 '18 at 03:55

1 Answers1

17

In Perl, this pattern is known as a modulino. I believe the term was coined by brian d foy in his book Mastering Perl. I don't often see the name applied for languages other than Perl, but it does happen.

Edit to add: the name goes back earlier than that. Here's an article from 2004 that uses the term.

mob
  • 117,087
  • 18
  • 149
  • 283
  • 1
    And before that name was suggested, they were called progmods (coined by tchrist?) – ysth Jul 05 '18 at 11:08
  • Thank you for mentioning about the great articles for modulino. But I guess we can back further ago about this technique. Because I found [this post by Tim Bunce on c.l.p.m](https://gist.github.com/hkoba/37ec19af3c7a362f1288367c8f2adf91) from my local mail archives. So, It was publically known at least from 1995. (And I personally used similar technique in some of my perl codes written at 1999-2000). – hkoba Jul 06 '18 at 15:13