Talk:Modulinos

From Rosetta Code

Needs more description

The task description here is missing something. Is this a task to show a main function that will be run from the command line? I've never heard the phrase "scripted main" before, so I'd like some more definition there too. The examples also seem to do lots of stuff that isn't described in the directions. --Mwn3d 00:16, 4 March 2011 (UTC)

Very much agree.
The Github link in the intro text says this:
Programs that only run main() if they're not loaded as libraries by other programs.
Make of that what you will. Clarification definitely needed. -- Erik Siers 07:16, 4 March 2011 (UTC)
I write command line programs with usage info, flags, etc. I like to combine the exported API with a usable program. Some programming languages have well-known syntax; Python's if __name__=="__main__": main() illustrates this behavior. Other languages, such as Common Lisp, require a hack. I want these hacks to be more easily Googled--I had trouble finding them in the first place. --Mcandre

Name change?

Reason being that ScriptedMain , even if changed to the more RC norm of "Scripted main", doesn't mean much, or as much as, say, "Executable library". The task seems to be about having a means of allowing library code - usually imported/included into a larger program; to also function as a useful program in its own right when executed directly.

Use whichever name you like. It's hard to search for this behavior online, so I've decided to give it an itinerant name, "Scripted main", the main() that runs only when a program is run directly, not imported by other programs. --Mcandre

The idea of an executable library (scripted main), has merit for interpreted languages, but seems contrived for compiled languages as in most cases you are further removed from both the library sources and the means to compile them, when actually executing a program using the library. Although many stand alone libraries are available in, for example, C, I have only seen stand-alone utilities that come with those libraries as separate programs with separate source files that link to the libraries at compile time. --Paddy3118 00:39, 4 March 2011 (UTC)

There is some truth to that. But many languages offer scripting and compilation. In any case, it's helpful to know that main() is always executed (C, C++, D), optionally executed (Perl, Python, Ruby, Chicken Scheme), or requires obscure syntactical tricks to even attempt (Bash, Lua, Common Lisp). I could omit the print-current-directory, print-current-program, and print-command-line-arguments sections from the scripted main examples, since Rosetta already has snippets for those behaviors. But programmers are likely to want scripted main and these other behaviors together, and often one is needed in order for the other to be possible. E.g., Chicken Scheme has no built-in way to get the program name, and scripted main won't work in compiled mode unless it can compare its command line arguments and program name.

TL;DR: All four behaviors are useful to know and they're very hard to Google. --Mcandre

... Which is why I would prefer a more descriptive name that might be more likely to be searched for, like "Executable library". --Paddy3118 12:35, 6 March 2011 (UTC)

Some chat in IRC

There was some discussion in IRC on the subject: http://irclog.perlgeek.de/rosettacode/2011-03-04#i_3355683. --Michael Mol 16:14, 4 March 2011 (UTC)

When a compiled program is run, the compiled program is run. The compiled program cannot be loaded as a library too can it? (I mean that it isn't done normally). If not then only normally interpreted languages could have this property; things like the C example don't fit the task description. --Paddy3118 16:33, 4 March 2011 (UTC)
It can; we do exactly this in one of my employer's products. I was rather surprised myself. I'd venture a guess that, at the OS level, this is exactly what's done with binary images anyway, except that the OS automatically runs a predefined entry point. (On Windows, I believe this is WinMain. I'm not sure what it is on Linux. The CRT wraps these in both cases.) The C example on the page is incorrect to that end, though. I believe the Java example is, too. --Michael Mol 17:37, 4 March 2011 (UTC)
I could change the task to reflect what Python does with, for example its CGIHTTPServer module, where calling it as command python -m CGIHTTPServer starts a web server, and loading it as a module would allow the calling program access to code for setting up a web server. --Paddy3118 16:33, 4 March 2011 (UTC)

Ambiguities

According to the page history, the C program was the first example, so it should serve to illustrate what this task is about. However, the C implementation has:

<lang c>#include "scriptedmain.h"</lang>

This whole concept of "startup" is an OS issue, and not a language issue. Some operating systems have various ways of starting programs, and "main" may or may not be a part of them. (For example, using drag&drop to start a program might work differently from starting the program from a command line.)

Furthermore, different concepts of what this all means depend very much on how the code is represented. A compiled language may be represented as a shared library, or not, where an interpreted language may be represented as neither. But a compiled language may also be represented as source code. And if you have source code as a set of files, you could perhaps simply ignore the file that defines main (if the language even uses that name -- some might abstract it away even if the OS requires its use).

So, anyways, I think this task needs some work. Specifically, I think it needs some kind of illustration requirement (where the "scripted main" code gets used by some other code that has its own "main").

-- Rdm 17:51, 4 March 2011 (UTC)

Test the C program yourself. As long as scriptedmain.h is missing the main() prototype, a second file test.c will not execute scriptedmain.c's main(). --Mcandre

I see that you have updated the description of the C program, to specify that scriptedmain.h is blank, so I have removed those comments that referred to this issue. However, since the example does not demonstrate the use of any API, I think the example is useless and unclear. (We have to imagine that we know what you are trying to illustrate.)
That said, my current impression is that you are asking us to illustrate putting the API in a separate file from the definition of "main". If that is the case, many of the examples here (including the one I wrote) are incorrect. --Rdm 13:49, 6 March 2011 (UTC)