=={{header|J}}==
scriptedmain.j:
Probably the simplest way to achive what I imagine "scripted main" to be, in J, involves the use of the [http://www.jsoftware.com/help/dictionary/dx009.htm Immex Phrase]. Here, just before the script ends, you define the "main" which would take control if the script was used as a stand alone program.
<lang j>#!/usr/bin/env jconsole
Here is an example "scripted main" program, using this approach:
load 'regex'
<lang j>NB. example "scripted main" code, saved as sm.ijs
myName=: 'My name is George'
9!:29]1
9!:27'smoutput myName'</lang>
meaningOfLife =: 42
Here is an example consumer, which is another "scripted main" program:
main =: monad : 0
<lang j>NB. example "alternate main" code
echo 'Main: The meaning of life is ', > 'd' (8!:0) meaningOfLife
require'sm.ijs'
exit ''
9!:29]1
)
9!:27'smoutput ''length: '',":#myName'</lang>
shouldrun =: monad : 0
Here is another example consumer. This example is library code, without any main:
if. '.*scriptedmain.*' rxeq > 1 { ARGV do.
main 0
end.
)
shouldrun 0</lang>
<lang j>NB. example "non main" library code
require'sm.ijs'
9!:29]0
reversed=:|.myName</lang>
test.j:
<lang j>#!/usr/bin/env load 'sm.ijs'jconsole
My name is George
load 'smalt.ijs'
length: 17
load 'smlib.ijs'
reversed
egroeG si eman yM</lang>
load 'scriptedmain.j'
This could also be used from the command line, for example <code>jconsole sm.ijs</code> and so on...
echo 'Test: The meaning of life is ', > 'd' (8!:0) meaningOfLife
exit ''</lang>
<lang sh>$ ./scriptedmain.j
Main: The meaning of life is 42
$ ./test.j
Test: The meaning of life is 42</lang>
=={{header|Java}}==
|