Modulinos: Difference between revisions

Content added Content deleted
m (note security holes around empty argv)
m (syntax highlighting fixup automation)
Line 14: Line 14:


=={{header|11l}}==
=={{header|11l}}==
<lang 11l>// life.11l
<syntaxhighlight lang="11l">// life.11l


F meaning_of_life()
F meaning_of_life()
Line 20: Line 20:


:start:
:start:
print(‘Main: The meaning of life is ’meaning_of_life())</lang>
print(‘Main: The meaning of life is ’meaning_of_life())</syntaxhighlight>


<lang 11l>// death.11l
<syntaxhighlight lang="11l">// death.11l


print(‘Life means ’life:meaning_of_life()‘.’)
print(‘Life means ’life:meaning_of_life()‘.’)
print(‘Death means nothing.’)</lang>
print(‘Death means nothing.’)</syntaxhighlight>


=={{header|AppleScript}}==
=={{header|AppleScript}}==
Line 31: Line 31:
AppleScript's equivalent of a main() function is a <tt>run</tt> handler, which can be either implicit or explicit:
AppleScript's equivalent of a main() function is a <tt>run</tt> handler, which can be either implicit or explicit:


<lang applescript>display dialog "Hello"</lang>
<syntaxhighlight lang="applescript">display dialog "Hello"</syntaxhighlight>
or
or
<lang applescript>on run
<syntaxhighlight lang="applescript">on run
display dialog "Hello"
display dialog "Hello"
end run</lang>
end run</syntaxhighlight>


A <tt>run</tt> handler's only executed when the script containing it is explicity ''run'', either from another script or application or as an application in its own right. It's not executed when a script's simply loaded as a library, although it can subsequently be so in the unlikely event of this being desirable. Scripts saved as applications aren't recognised by the "Libraries" system introduced in Mac OS X 10.9, but can be loaded and/or run using the older <tt>load script</tt> and <tt>run script</tt> commands. Script code can tell if it's running in its own application or being executed by an external agent by comparing its file path with that of the agent:
A <tt>run</tt> handler's only executed when the script containing it is explicity ''run'', either from another script or application or as an application in its own right. It's not executed when a script's simply loaded as a library, although it can subsequently be so in the unlikely event of this being desirable. Scripts saved as applications aren't recognised by the "Libraries" system introduced in Mac OS X 10.9, but can be loaded and/or run using the older <tt>load script</tt> and <tt>run script</tt> commands. Script code can tell if it's running in its own application or being executed by an external agent by comparing its file path with that of the agent:


<lang applescript>on run
<syntaxhighlight lang="applescript">on run
if ((path to me) = (path to current application)) then
if ((path to me) = (path to current application)) then
display dialog "I'm running in my own application."
display dialog "I'm running in my own application."
Line 45: Line 45:
display dialog "I'm being run from another script or application."
display dialog "I'm being run from another script or application."
end if
end if
end run</lang>
end run</syntaxhighlight>


=={{header|Arturo}}==
=={{header|Arturo}}==
Line 51: Line 51:
===Library===
===Library===


<lang rebol>; modulinos - library
<syntaxhighlight lang="rebol">; modulinos - library
meaningOfLife: function [][
meaningOfLife: function [][
Line 58: Line 58:


if standalone? ->
if standalone? ->
print ~"Library: The meaning of life is |meaningOfLife|"</lang>
print ~"Library: The meaning of life is |meaningOfLife|"</syntaxhighlight>


{{out}}
{{out}}
Line 66: Line 66:
===Main===
===Main===


<lang rebol>do.import relative "modulinos - library.art"
<syntaxhighlight lang="rebol">do.import relative "modulinos - library.art"
print ~"Life means |meaningOfLife|."
print ~"Life means |meaningOfLife|."
print "Death means invisible scary skeletons."</lang>
print "Death means invisible scary skeletons."</syntaxhighlight>


{{out}}
{{out}}
Line 84: Line 84:
Example
Example


<lang sh>$ make
<syntaxhighlight lang="sh">$ make
./scriptedmain
./scriptedmain
Main: The meaning of life is 42
Main: The meaning of life is 42
./test
./test
Test: The meaning of life is</lang>
Test: The meaning of life is</syntaxhighlight>


Makefile
Makefile


<lang make>all: scriptedmain test
<syntaxhighlight lang="make">all: scriptedmain test
./scriptedmain
./scriptedmain
./test
./test
Line 106: Line 106:
-rm test
-rm test
-rm scriptedmain.exe
-rm scriptedmain.exe
-rm test.exe</lang>
-rm test.exe</syntaxhighlight>


scriptedmain.h
scriptedmain.h


<lang c>int meaning_of_life();</lang>
<syntaxhighlight lang="c">int meaning_of_life();</syntaxhighlight>


scriptedmain.c
scriptedmain.c


<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


int meaning_of_life() {
int meaning_of_life() {
Line 128: Line 128:
}
}


#endif</lang>
#endif</syntaxhighlight>


test.c
test.c


<lang c>#include "scriptedmain.h"
<syntaxhighlight lang="c">#include "scriptedmain.h"
#include <stdio.h>
#include <stdio.h>


Line 140: Line 140:
printf("Test: The meaning of life is %d\n", meaning_of_life());
printf("Test: The meaning of life is %d\n", meaning_of_life());
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
Line 147: Line 147:
Example
Example


<lang sh>$ make
<syntaxhighlight lang="sh">$ make
./scriptedmain
./scriptedmain
Main: The meaning of life is 42
Main: The meaning of life is 42
./test
./test
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


Makefile
Makefile


<lang make>all: scriptedmain test
<syntaxhighlight lang="make">all: scriptedmain test
./scriptedmain
./scriptedmain
./test
./test
Line 169: Line 169:
-rm test
-rm test
-rm scriptedmain.exe
-rm scriptedmain.exe
-rm test.exe</lang>
-rm test.exe</syntaxhighlight>


scriptedmain.h
scriptedmain.h


<lang cpp>int meaning_of_life();</lang>
<syntaxhighlight lang="cpp">int meaning_of_life();</syntaxhighlight>


scriptedmain.cpp
scriptedmain.cpp


<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>


using namespace std;
using namespace std;
Line 192: Line 192:
}
}


#endif</lang>
#endif</syntaxhighlight>


test.cpp
test.cpp


<lang cpp>#include "scriptedmain.h"
<syntaxhighlight lang="cpp">#include "scriptedmain.h"
#include <iostream>
#include <iostream>


Line 206: Line 206:
cout << "Test: The meaning of life is " << meaning_of_life() << endl;
cout << "Test: The meaning of life is " << meaning_of_life() << endl;
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 212: Line 212:


scriptedmain.clj:
scriptedmain.clj:
<lang clojure>":";exec lein exec $0 ${1+"$@"}
<syntaxhighlight lang="clojure">":";exec lein exec $0 ${1+"$@"}
":";exit
":";exit


Line 224: Line 224:


(when (.contains (first *command-line-args*) *source-path*)
(when (.contains (first *command-line-args*) *source-path*)
(apply -main (rest *command-line-args*)))</lang>
(apply -main (rest *command-line-args*)))</syntaxhighlight>


test.clj:
test.clj:
<lang clojure>":";exec lein exec $0 ${1+"$@"}
<syntaxhighlight lang="clojure">":";exec lein exec $0 ${1+"$@"}
":";exit
":";exit


Line 239: Line 239:


(when (.contains (first *command-line-args*) *source-path*)
(when (.contains (first *command-line-args*) *source-path*)
(apply -main (rest *command-line-args*)))</lang>
(apply -main (rest *command-line-args*)))</syntaxhighlight>


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==
scriptedmain.coffee:
scriptedmain.coffee:
<lang coffeescript>#!/usr/bin/env coffee
<syntaxhighlight lang="coffeescript">#!/usr/bin/env coffee


meaningOfLife = () -> 42
meaningOfLife = () -> 42
Line 252: Line 252:
console.log "Main: The meaning of life is " + meaningOfLife()
console.log "Main: The meaning of life is " + meaningOfLife()


if not module.parent then main()</lang>
if not module.parent then main()</syntaxhighlight>


test.coffee:
test.coffee:
<lang coffeescript>#!/usr/bin/env coffee
<syntaxhighlight lang="coffeescript">#!/usr/bin/env coffee


sm = require "./scriptedmain"
sm = require "./scriptedmain"


console.log "Test: The meaning of life is " + sm.meaningOfLife()</lang>
console.log "Test: The meaning of life is " + sm.meaningOfLife()</syntaxhighlight>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 268: Line 268:
~/.clisprc.lisp
~/.clisprc.lisp


<lang lisp>;;; Play nice with shebangs
<syntaxhighlight lang="lisp">;;; Play nice with shebangs
(set-dispatch-macro-character #\# #\!
(set-dispatch-macro-character #\# #\!
(lambda (stream character n)
(lambda (stream character n)
(declare (ignore character n))
(declare (ignore character n))
(read-line stream nil nil t)
(read-line stream nil nil t)
nil))</lang>
nil))</syntaxhighlight>


scriptedmain.lisp
scriptedmain.lisp


<lang lisp>#!/bin/sh
<syntaxhighlight lang="lisp">#!/bin/sh
#|
#|
exec clisp -q -q $0 $0 ${1+"$@"}
exec clisp -q -q $0 $0 ${1+"$@"}
Line 307: Line 307:
args
args
:test #'(lambda (x y) (search x y :test #'equalp)))
:test #'(lambda (x y) (search x y :test #'equalp)))
(main args)))</lang>
(main args)))</syntaxhighlight>


test.lisp
test.lisp


<lang lisp>#!/bin/sh
<syntaxhighlight lang="lisp">#!/bin/sh
#|
#|
exec clisp -q -q $0 $0 ${1+"$@"}
exec clisp -q -q $0 $0 ${1+"$@"}
Line 318: Line 318:


(load "scriptedmain.lisp")
(load "scriptedmain.lisp")
(format t "Test: The meaning of life is ~a~%" (meaning-of-life))</lang>
(format t "Test: The meaning of life is ~a~%" (meaning-of-life))</syntaxhighlight>


=={{header|D}}==
=={{header|D}}==
Line 326: Line 326:
scriptedmain.d:
scriptedmain.d:


<lang d>#!/usr/bin/env rdmd -version=scriptedmain
<syntaxhighlight lang="d">#!/usr/bin/env rdmd -version=scriptedmain


module scriptedmain;
module scriptedmain;
Line 340: Line 340:
writeln("Main: The meaning of life is ", meaningOfLife());
writeln("Main: The meaning of life is ", meaningOfLife());
}
}
}</lang>
}</syntaxhighlight>


test.d:
test.d:


<lang d>#!/usr/bin/env rdmd -version=test
<syntaxhighlight lang="d">#!/usr/bin/env rdmd -version=test


import scriptedmain;
import scriptedmain;
Line 353: Line 353:
writeln("Test: The meaning of life is ", meaningOfLife());
writeln("Test: The meaning of life is ", meaningOfLife());
}
}
}</lang>
}</syntaxhighlight>


Example:
Example:


<lang sh>$ ./scriptedmain.d
<syntaxhighlight lang="sh">$ ./scriptedmain.d
Main: The meaning of life is 42
Main: The meaning of life is 42
$ ./test.d
$ ./test.d
Line 366: Line 366:
$ dmd test.d scriptedmain.d -version=test
$ dmd test.d scriptedmain.d -version=test
$ ./test
$ ./test
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


=={{header|Dart}}==
=={{header|Dart}}==
scriptedmain.dart:
scriptedmain.dart:
<lang dart>#!/usr/bin/env dart
<syntaxhighlight lang="dart">#!/usr/bin/env dart


#library("scriptedmain");
#library("scriptedmain");
Line 380: Line 380:
main() {
main() {
print("Main: The meaning of life is ${meaningOfLife()}");
print("Main: The meaning of life is ${meaningOfLife()}");
}</lang>
}</syntaxhighlight>


test.dart:
test.dart:
<lang dart>#!/usr/bin/env dart
<syntaxhighlight lang="dart">#!/usr/bin/env dart


#import("scriptedmain.dart", prefix: "scriptedmain");
#import("scriptedmain.dart", prefix: "scriptedmain");
Line 389: Line 389:
main() {
main() {
print("Test: The meaning of life is ${scriptedmain.meaningOfLife()}");
print("Test: The meaning of life is ${scriptedmain.meaningOfLife()}");
}</lang>
}</syntaxhighlight>


Example:
Example:
<lang sh>$ ./scriptedmain.dart
<syntaxhighlight lang="sh">$ ./scriptedmain.dart
Main: The meaning of life is 42
Main: The meaning of life is 42
$ ./test.dart
$ ./test.dart
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
Line 402: Line 402:
scriptedmain.el
scriptedmain.el


<lang lisp>:;exec emacs -batch -l $0 -f main $*
<syntaxhighlight lang="lisp">:;exec emacs -batch -l $0 -f main $*


;;; Shebang from John Swaby
;;; Shebang from John Swaby
Line 410: Line 410:


(defun main ()
(defun main ()
(message "Main: The meaning of life is %d" (meaning-of-life)))</lang>
(message "Main: The meaning of life is %d" (meaning-of-life)))</syntaxhighlight>


test.el
test.el


<lang lisp>:;exec emacs -batch -l $0 -f main $*
<syntaxhighlight lang="lisp">:;exec emacs -batch -l $0 -f main $*


;;; Shebang from John Swaby
;;; Shebang from John Swaby
Line 422: Line 422:
(setq load-path (cons default-directory load-path))
(setq load-path (cons default-directory load-path))
(load "scriptedmain.el" nil t)
(load "scriptedmain.el" nil t)
(message "Test: The meaning of life is %d" (meaning-of-life)))</lang>
(message "Test: The meaning of life is %d" (meaning-of-life)))</syntaxhighlight>


=={{header|Erlang}}==
=={{header|Erlang}}==
Line 428: Line 428:


Makefile:
Makefile:
<lang make>all: t
<syntaxhighlight lang="make">all: t


t: scriptedmain.beam test.beam
t: scriptedmain.beam test.beam
Line 441: Line 441:


clean:
clean:
-rm *.beam</lang>
-rm *.beam</syntaxhighlight>


scriptedmain.erl:
scriptedmain.erl:
<lang erlang>-module(scriptedmain).
<syntaxhighlight lang="erlang">-module(scriptedmain).
-export([meaning_of_life/0, start/0]).
-export([meaning_of_life/0, start/0]).


Line 451: Line 451:
start() ->
start() ->
io:format("Main: The meaning of life is ~w~n", [meaning_of_life()]),
io:format("Main: The meaning of life is ~w~n", [meaning_of_life()]),
init:stop().</lang>
init:stop().</syntaxhighlight>


test.erl:
test.erl:
<lang erlang>-module(test).
<syntaxhighlight lang="erlang">-module(test).
-export([start/0]).
-export([start/0]).
-import(scriptedmain, [meaning_of_life/0]).
-import(scriptedmain, [meaning_of_life/0]).
Line 460: Line 460:
start() ->
start() ->
io:format("Test: The meaning of life is ~w~n", [meaning_of_life()]),
io:format("Test: The meaning of life is ~w~n", [meaning_of_life()]),
init:stop().</lang>
init:stop().</syntaxhighlight>


=={{header|F Sharp|F#}}==
=={{header|F Sharp|F#}}==
Line 474: Line 474:
Example:
Example:


<lang sh>$ make
<syntaxhighlight lang="sh">$ make
fsharpc --out:scriptedmain.exe ScriptedMain.fs
fsharpc --out:scriptedmain.exe ScriptedMain.fs
fsharpc --out:test.exe ScriptedMain.fs Test.fs
fsharpc --out:test.exe ScriptedMain.fs Test.fs
Line 480: Line 480:
Main: The meaning of life is 42
Main: The meaning of life is 42
$ mono test.exe
$ mono test.exe
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


Makefile:
Makefile:


<lang make>all: scriptedmain.exe test.exe
<syntaxhighlight lang="make">all: scriptedmain.exe test.exe


scriptedmain.exe: ScriptedMain.fs
scriptedmain.exe: ScriptedMain.fs
Line 493: Line 493:


clean:
clean:
-rm *.exe</lang>
-rm *.exe</syntaxhighlight>


ScriptedMain.fs:
ScriptedMain.fs:


<lang fsharp>namespace ScriptedMain
<syntaxhighlight lang="fsharp">namespace ScriptedMain


module ScriptedMain =
module ScriptedMain =
Line 503: Line 503:


let main =
let main =
printfn "Main: The meaning of life is %d" meaningOfLife</lang>
printfn "Main: The meaning of life is %d" meaningOfLife</syntaxhighlight>


Test.fs:
Test.fs:


<lang fsharp>module Test =
<syntaxhighlight lang="fsharp">module Test =
open ScriptedMain
open ScriptedMain


let main =
let main =
printfn "Test: The meaning of life is %d" ScriptedMain.meaningOfLife</lang>
printfn "Test: The meaning of life is %d" ScriptedMain.meaningOfLife</syntaxhighlight>


=={{header|Factor}}==
=={{header|Factor}}==
Line 518: Line 518:
Example:
Example:


<lang sh>$ ./scriptedmain.factor
<syntaxhighlight lang="sh">$ ./scriptedmain.factor
Main: The meaning of life is 42
Main: The meaning of life is 42
$ ./test.factor
$ ./test.factor
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


~/.factor-rc:
~/.factor-rc:


<lang factor>! INCLUDING macro that imports source code files in the current directory
<syntaxhighlight lang="factor">! INCLUDING macro that imports source code files in the current directory


USING: kernel vocabs.loader parser sequences lexer vocabs.parser ;
USING: kernel vocabs.loader parser sequences lexer vocabs.parser ;
Line 532: Line 532:
: include-vocab ( vocab -- ) dup ".factor" append parse-file append use-vocab ;
: include-vocab ( vocab -- ) dup ".factor" append parse-file append use-vocab ;


SYNTAX: INCLUDING: ";" [ include-vocab ] each-token ;</lang>
SYNTAX: INCLUDING: ";" [ include-vocab ] each-token ;</syntaxhighlight>


scriptedmain.factor:
scriptedmain.factor:


<lang factor>#! /usr/bin/env factor
<syntaxhighlight lang="factor">#! /usr/bin/env factor


USING: io math.parser ;
USING: io math.parser ;
Line 545: Line 545:
: main ( -- ) meaning-of-life "Main: The meaning of life is " write number>string print ;
: main ( -- ) meaning-of-life "Main: The meaning of life is " write number>string print ;


MAIN: main</lang>
MAIN: main</syntaxhighlight>


test.factor:
test.factor:


<lang factor>#! /usr/bin/env factor
<syntaxhighlight lang="factor">#! /usr/bin/env factor


INCLUDING: scriptedmain ;
INCLUDING: scriptedmain ;
Line 557: Line 557:
: main ( -- ) meaning-of-life "Test: The meaning of life is " write number>string print ;
: main ( -- ) meaning-of-life "Test: The meaning of life is " write number>string print ;


MAIN: main</lang>
MAIN: main</syntaxhighlight>


=={{header|Forth}}==
=={{header|Forth}}==
Line 563: Line 563:
Given this awful running reference:
Given this awful running reference:


<lang forth>42 constant Douglas-Adams
<syntaxhighlight lang="forth">42 constant Douglas-Adams


: go ( -- )
: go ( -- )
." The meaning of life is " Douglas-Adams . cr ;</lang>
." The meaning of life is " Douglas-Adams . cr ;</syntaxhighlight>


The bulk of Forth systems provide a way to generate an executable that enters GO (ar any word) on start.
The bulk of Forth systems provide a way to generate an executable that enters GO (ar any word) on start.
Line 572: Line 572:
{{works with|SwiftForth|SwiftForth|4.0}}
{{works with|SwiftForth|SwiftForth|4.0}}


<lang forth>' go 'MAIN !
<syntaxhighlight lang="forth">' go 'MAIN !
program douglas-adams</lang>
program douglas-adams</syntaxhighlight>


Which creates a file named 'douglas-adams' that you can then run. If this is all in the same file, you can load the file, test parts of it, and then exit (or shell out) to run the executable.
Which creates a file named 'douglas-adams' that you can then run. If this is all in the same file, you can load the file, test parts of it, and then exit (or shell out) to run the executable.
Line 581: Line 581:
{{works with|gforth}}
{{works with|gforth}}


<lang forth>#! /usr/bin/env gforth
<syntaxhighlight lang="forth">#! /usr/bin/env gforth


42 constant Douglas-Adams
42 constant Douglas-Adams
.( The meaning of life is ) Douglas-Adams . cr bye</lang>
.( The meaning of life is ) Douglas-Adams . cr bye</syntaxhighlight>


Adding #! as a comment, as gforth does, is trivial. For a means by which this script could distinguish between 'scripted execution' and otherwise, a symlink like 'forthscript' could easily be used, and the zeroth OS argument tested for, but there's no convention.
Adding #! as a comment, as gforth does, is trivial. For a means by which this script could distinguish between 'scripted execution' and otherwise, a symlink like 'forthscript' could easily be used, and the zeroth OS argument tested for, but there's no convention.
Line 590: Line 590:
{{works with|gforth}}
{{works with|gforth}}


<lang forth>#! /usr/bin/env forthscript
<syntaxhighlight lang="forth">#! /usr/bin/env forthscript


42 constant Douglas-Adams
42 constant Douglas-Adams
Line 598: Line 598:
[THEN]
[THEN]


cr .( Why aren't you running this as a script? It only provides a constant.)</lang>
cr .( Why aren't you running this as a script? It only provides a constant.)</syntaxhighlight>




=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|Ring}}
{{trans|Ring}}
<lang freebasic>
<syntaxhighlight lang="freebasic">
Function meaningoflife() As Byte
Function meaningoflife() As Byte
Dim As Byte y = 42
Dim As Byte y = 42
Line 615: Line 615:
main()
main()
Sleep
Sleep
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 628: Line 628:


First create these two files in the 'modulino' directory:
First create these two files in the 'modulino' directory:
<lang go>// modulino.go
<syntaxhighlight lang="go">// modulino.go
package main
package main


Line 639: Line 639:
func libMain() {
func libMain() {
fmt.Println("The meaning of life is", MeaningOfLife())
fmt.Println("The meaning of life is", MeaningOfLife())
}</lang>
}</syntaxhighlight>


<lang go>// modulino_main.go
<syntaxhighlight lang="go">// modulino_main.go
package main
package main
func main() {
func main() {
libMain()
libMain()
}</lang>
}</syntaxhighlight>


To emulate a modulino:
To emulate a modulino:
Line 657: Line 657:


Now create this file in the 'mol' directory:
Now create this file in the 'mol' directory:
<lang go>// mol.go
<syntaxhighlight lang="go">// mol.go
package main
package main


Line 664: Line 664:
func main() {
func main() {
fmt.Println("The meaning of life is still", MeaningOfLife())
fmt.Println("The meaning of life is still", MeaningOfLife())
}</lang>
}</syntaxhighlight>
and copy modulino.go to the 'mol' directory. The library can then be used in the 'normal' way:
and copy modulino.go to the 'mol' directory. The library can then be used in the 'normal' way:
{{out}}
{{out}}
Line 677: Line 677:
Example:
Example:


<lang sh>$ ./ScriptedMain.groovy
<syntaxhighlight lang="sh">$ ./ScriptedMain.groovy
Main: The meaning of life is 42
Main: The meaning of life is 42
$ ./Test.groovy
$ ./Test.groovy
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


ScriptedMain.groovy:
ScriptedMain.groovy:


<lang groovy>#!/usr/bin/env groovy
<syntaxhighlight lang="groovy">#!/usr/bin/env groovy


class ScriptedMain {
class ScriptedMain {
Line 692: Line 692:
println "Main: The meaning of life is " + meaningOfLife
println "Main: The meaning of life is " + meaningOfLife
}
}
}</lang>
}</syntaxhighlight>


Test.groovy:
Test.groovy:


<lang groovy>#!/usr/bin/env groovy
<syntaxhighlight lang="groovy">#!/usr/bin/env groovy


println "Test: The meaning of life is " + ScriptedMain.meaningOfLife</lang>
println "Test: The meaning of life is " + ScriptedMain.meaningOfLife</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
Haskell has scripted main, but getting scripted main to work with compiled scripts is tricky.
Haskell has scripted main, but getting scripted main to work with compiled scripts is tricky.


<lang sh>$ runhaskell scriptedmain.hs
<syntaxhighlight lang="sh">$ runhaskell scriptedmain.hs
Main: The meaning of life is 42
Main: The meaning of life is 42
$ runhaskell test.hs
$ runhaskell test.hs
Line 712: Line 712:
$ ghc -fforce-recomp -o test -main-is Test test.hs scriptedmain.hs
$ ghc -fforce-recomp -o test -main-is Test test.hs scriptedmain.hs
$ ./test
$ ./test
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


scriptedmain.hs
scriptedmain.hs


<lang haskell>#!/usr/bin/env runhaskell
<syntaxhighlight lang="haskell">#!/usr/bin/env runhaskell


-- Compile:
-- Compile:
Line 728: Line 728:


main :: IO ()
main :: IO ()
main = putStrLn $ "Main: The meaning of life is " ++ show meaningOfLife</lang>
main = putStrLn $ "Main: The meaning of life is " ++ show meaningOfLife</syntaxhighlight>


test.hs
test.hs


<lang haskell>#!/usr/bin/env runhaskell
<syntaxhighlight lang="haskell">#!/usr/bin/env runhaskell


-- Compile:
-- Compile:
Line 743: Line 743:


main :: IO ()
main :: IO ()
main = putStrLn $ "Test: The meaning of life is " ++ show meaningOfLife</lang>
main = putStrLn $ "Test: The meaning of life is " ++ show meaningOfLife</syntaxhighlight>


=={{header|Io}}==
=={{header|Io}}==
Line 749: Line 749:
ScriptedMain.io:
ScriptedMain.io:


<lang io>#!/usr/bin/env io
<syntaxhighlight lang="io">#!/usr/bin/env io


ScriptedMain := Object clone
ScriptedMain := Object clone
Line 756: Line 756:
if( isLaunchScript,
if( isLaunchScript,
"Main: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println
"Main: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println
)</lang>
)</syntaxhighlight>


test.io:
test.io:


<lang io>#!/usr/bin/env io
<syntaxhighlight lang="io">#!/usr/bin/env io


"Test: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println</lang>
"Test: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println</syntaxhighlight>


<lang sh>$ ./ScriptedMain.io
<syntaxhighlight lang="sh">$ ./ScriptedMain.io
Main: The meaning of life is 42
Main: The meaning of life is 42
$ ./test.io
$ ./test.io
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
Line 773: Line 773:
modulinos.ijs:
modulinos.ijs:


<lang j>#!/usr/bin/env ijconsole
<syntaxhighlight lang="j">#!/usr/bin/env ijconsole
meaningOfLife =: 42
meaningOfLife =: 42
Line 788: Line 788:
)
)
shouldrun 0</lang>
shouldrun 0</syntaxhighlight>


test.j:
test.j:


<lang j>#!/usr/bin/env jconsole
<syntaxhighlight lang="j">#!/usr/bin/env jconsole


load 'modulinos.ijs'
load 'modulinos.ijs'
Line 798: Line 798:
echo 'Test: The meaning of life is ',": meaningOfLife
echo 'Test: The meaning of life is ',": meaningOfLife


exit ''</lang>
exit ''</syntaxhighlight>


Example:
Example:


<lang sh>$ ./modulinos.ijs
<syntaxhighlight lang="sh">$ ./modulinos.ijs
Main: The meaning of life is 42
Main: The meaning of life is 42
$ ./test.j
$ ./test.j
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
Line 812: Line 812:
ScriptedMain.java
ScriptedMain.java


<lang java>public class ScriptedMain {
<syntaxhighlight lang="java">public class ScriptedMain {
public static int meaningOfLife() {
public static int meaningOfLife() {
return 42;
return 42;
Line 820: Line 820:
System.out.println("Main: The meaning of life is " + meaningOfLife());
System.out.println("Main: The meaning of life is " + meaningOfLife());
}
}
}</lang>
}</syntaxhighlight>


Test.java
Test.java


<lang java>public class Test {
<syntaxhighlight lang="java">public class Test {
public static void main(String[] args) {
public static void main(String[] args) {
System.out.println("Test: The meaning of life is " + ScriptedMain.meaningOfLife());
System.out.println("Test: The meaning of life is " + ScriptedMain.meaningOfLife());
}
}
}</lang>
}</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
Line 835: Line 835:


scriptedmain.js
scriptedmain.js
<lang javascript>#!/usr/bin/env node
<syntaxhighlight lang="javascript">#!/usr/bin/env node


function meaningOfLife() { return 42; }
function meaningOfLife() { return 42; }
Line 845: Line 845:
}
}


if (!module.parent) { main(); }</lang>
if (!module.parent) { main(); }</syntaxhighlight>


test.js
test.js
<lang javascript>#!/usr/bin/env node
<syntaxhighlight lang="javascript">#!/usr/bin/env node


var sm = require("./scriptedmain");
var sm = require("./scriptedmain");


console.log("Test: The meaning of life is " + sm.meaningOfLife());</lang>
console.log("Test: The meaning of life is " + sm.meaningOfLife());</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
Line 858: Line 858:
<br />
<br />
In module file Divisors.jl:
In module file Divisors.jl:
<lang julia>module Divisors
<syntaxhighlight lang="julia">module Divisors


using Primes
using Primes
Line 899: Line 899:
Divisors.interactiveDivisors()
Divisors.interactiveDivisors()
end
end
</syntaxhighlight>
</lang>
In a user file getdivisors.jl:
In a user file getdivisors.jl:
<lang julia>include("divisors.jl")
<syntaxhighlight lang="julia">include("divisors.jl")


using .Divisors
using .Divisors
Line 907: Line 907:
n = 708245926330
n = 708245926330
println("The proper divisors of $n are ", properdivisors(n))
println("The proper divisors of $n are ", properdivisors(n))
</syntaxhighlight>
</lang>


=={{header|LLVM}}==
=={{header|LLVM}}==
LLVM can have scripted main a la C, using the weak attribute.
LLVM can have scripted main a la C, using the weak attribute.


<lang sh>$ make
<syntaxhighlight lang="sh">$ make
llvm-as scriptedmain.ll
llvm-as scriptedmain.ll
llc scriptedmain.bc
llc scriptedmain.bc
Line 922: Line 922:
gcc -o test test.s scriptedmain.s
gcc -o test test.s scriptedmain.s
./test
./test
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


Makefile
Makefile


<lang make>EXECUTABLE_SM=scriptedmain
<syntaxhighlight lang="make">EXECUTABLE_SM=scriptedmain
EXECUTABLE_TEST=test
EXECUTABLE_TEST=test


Line 947: Line 947:
-rm test.bc
-rm test.bc
-rm scriptedmain.s
-rm scriptedmain.s
-rm scriptedmain.bc</lang>
-rm scriptedmain.bc</syntaxhighlight>


scriptedmain.ll
scriptedmain.ll


<lang llvm>@msg_main = internal constant [33 x i8] c"Main: The meaning of life is %d\0A\00"
<syntaxhighlight lang="llvm">@msg_main = internal constant [33 x i8] c"Main: The meaning of life is %d\0A\00"


declare i32 @printf(i8* noalias nocapture, ...)
declare i32 @printf(i8* noalias nocapture, ...)
Line 965: Line 965:


ret i32 0
ret i32 0
}</lang>
}</syntaxhighlight>


test.ll
test.ll


<lang llvm>@msg_test = internal constant [33 x i8] c"Test: The meaning of life is %d\0A\00"
<syntaxhighlight lang="llvm">@msg_test = internal constant [33 x i8] c"Test: The meaning of life is %d\0A\00"


declare i32 @printf(i8* noalias nocapture, ...)
declare i32 @printf(i8* noalias nocapture, ...)
Line 981: Line 981:


ret i32 0
ret i32 0
}</lang>
}</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==
Line 988: Line 988:
scriptedmain.lua
scriptedmain.lua


<lang lua>#!/usr/bin/env lua
<syntaxhighlight lang="lua">#!/usr/bin/env lua


function meaningoflife()
function meaningoflife()
Line 1,002: Line 1,002:
else
else
module(..., package.seeall)
module(..., package.seeall)
end</lang>
end</syntaxhighlight>


test.lua
test.lua


<lang lua>#!/usr/bin/env lua
<syntaxhighlight lang="lua">#!/usr/bin/env lua
sm = require("scriptedmain")
sm = require("scriptedmain")
print("Test: The meaning of life is " .. sm.meaningoflife())</lang>
print("Test: The meaning of life is " .. sm.meaningoflife())</syntaxhighlight>


=={{header|Make}}==
=={{header|Make}}==


Example
Example
<lang sh>$ make -f scriptedmain.mf
<syntaxhighlight lang="sh">$ make -f scriptedmain.mf
The meaning of life is 42
The meaning of life is 42
(Main)
(Main)
$ make -f test.mf
$ make -f test.mf
The meaning of life is 42
The meaning of life is 42
(Test)</lang>
(Test)</syntaxhighlight>


scriptedmain.mf
scriptedmain.mf
<lang make>all: scriptedmain
<syntaxhighlight lang="make">all: scriptedmain


meaning-of-life:
meaning-of-life:
Line 1,028: Line 1,028:
scriptedmain: meaning-of-life
scriptedmain: meaning-of-life
@echo "(Main)"
@echo "(Main)"
</syntaxhighlight>
</lang>


test.mf
test.mf
<lang make>all: test
<syntaxhighlight lang="make">all: test


test:
test:
@make -f scriptedmain.mf meaning-of-life
@make -f scriptedmain.mf meaning-of-life
@echo "(Test)"
@echo "(Test)"
</syntaxhighlight>
</lang>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==


scriptedmain.ma
scriptedmain.ma
<lang mathematica>#!/usr/bin/env MathKernel -script
<syntaxhighlight lang="mathematica">#!/usr/bin/env MathKernel -script


MeaningOfLife[] = 42
MeaningOfLife[] = 42
Line 1,056: Line 1,056:
If[StringMatchQ[Program, ".*scriptedmain.*"],
If[StringMatchQ[Program, ".*scriptedmain.*"],
Print["Main: The meaning of life is " <> ToString[MeaningOfLife[]]]
Print["Main: The meaning of life is " <> ToString[MeaningOfLife[]]]
]</lang>
]</syntaxhighlight>


test.ma:
test.ma:
<lang mathematica>#!/usr/bin/env MathKernel -script
<syntaxhighlight lang="mathematica">#!/usr/bin/env MathKernel -script


Get["scriptedmain.ma"]
Get["scriptedmain.ma"]


Print["Test: The meaning of life is " <> ToString[MeaningOfLife[]]]</lang>
Print["Test: The meaning of life is " <> ToString[MeaningOfLife[]]]</syntaxhighlight>


Example:
Example:
<lang sh>$ ./scriptedmain.ma
<syntaxhighlight lang="sh">$ ./scriptedmain.ma
Main: The meaning of life is 42
Main: The meaning of life is 42
$ ./test.ma
$ ./test.ma
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


In Mac and Windows, the output will be surrounded by spurious quotes.
In Mac and Windows, the output will be surrounded by spurious quotes.
Line 1,075: Line 1,075:
=={{header|Mozart/Oz}}==
=={{header|Mozart/Oz}}==
Makefile:
Makefile:
<lang make>all: run
<syntaxhighlight lang="make">all: run


run: scriptedmain test
run: scriptedmain test
Line 1,095: Line 1,095:
-rm *.ozf
-rm *.ozf
-rm *.exe
-rm *.exe
</syntaxhighlight>
</lang>


scriptedmain.oz:
scriptedmain.oz:
<lang oz>functor
<syntaxhighlight lang="oz">functor
export
export
meaningOfLife: MeaningOfLife
meaningOfLife: MeaningOfLife
Line 1,116: Line 1,116:
end
end
end
end
</syntaxhighlight>
</lang>


test.oz:
test.oz:
<lang oz>functor
<syntaxhighlight lang="oz">functor
import
import
ScriptedMain
ScriptedMain
Line 1,133: Line 1,133:
end
end
end
end
end</lang>
end</syntaxhighlight>


=={{header|newLISP}}==
=={{header|newLISP}}==
Line 1,140: Line 1,140:
scriptedmain.lsp
scriptedmain.lsp


<lang lisp>#!/usr/bin/env newlisp
<syntaxhighlight lang="lisp">#!/usr/bin/env newlisp


(context 'SM)
(context 'SM)
Line 1,152: Line 1,152:
(if (find "scriptedmain" (main-args 1)) (main))
(if (find "scriptedmain" (main-args 1)) (main))


(context MAIN)</lang>
(context MAIN)</syntaxhighlight>


test.lsp
test.lsp


<lang lisp>#!/usr/bin/env newlisp
<syntaxhighlight lang="lisp">#!/usr/bin/env newlisp


(load "scriptedmain.lsp")
(load "scriptedmain.lsp")
(println (format "Test: The meaning of life is %d" (SM:meaning-of-life)))
(println (format "Test: The meaning of life is %d" (SM:meaning-of-life)))
(exit)</lang>
(exit)</syntaxhighlight>


=={{header|Objective-C}}==
=={{header|Objective-C}}==
Line 1,166: Line 1,166:
scriptedmain.h:
scriptedmain.h:


<lang objc>#import <objc/Object.h>
<syntaxhighlight lang="objc">#import <objc/Object.h>


@interface ScriptedMain: Object {}
@interface ScriptedMain: Object {}
Line 1,172: Line 1,172:
+ (int)meaningOfLife;
+ (int)meaningOfLife;


@end</lang>
@end</syntaxhighlight>


scriptedmain.m:
scriptedmain.m:


<lang objc>#import "scriptedmain.h"
<syntaxhighlight lang="objc">#import "scriptedmain.h"
#import <Foundation/Foundation.h>
#import <Foundation/Foundation.h>


Line 1,195: Line 1,195:


return 0;
return 0;
}</lang>
}</syntaxhighlight>


test.m:
test.m:


<lang objc>#import "scriptedmain.h"
<syntaxhighlight lang="objc">#import "scriptedmain.h"
#import <Foundation/Foundation.h>
#import <Foundation/Foundation.h>


Line 1,210: Line 1,210:


return 0;
return 0;
}</lang>
}</syntaxhighlight>


<lang sh>$ gcc -o scriptedmain -lobjc -framework foundation scriptedmain.m
<syntaxhighlight lang="sh">$ gcc -o scriptedmain -lobjc -framework foundation scriptedmain.m
$ gcc -o test -lobjc -framework foundation test.m scriptedmain.m
$ gcc -o test -lobjc -framework foundation test.m scriptedmain.m
$ ./scriptedmain
$ ./scriptedmain
Main: The meaning of life is 42
Main: The meaning of life is 42
$ ./test
$ ./test
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==
Line 1,223: Line 1,223:
scriptedmain.ml
scriptedmain.ml


<lang ocaml>let meaning_of_life = 42
<syntaxhighlight lang="ocaml">let meaning_of_life = 42


let main () =
let main () =
Line 1,231: Line 1,231:
let () =
let () =
if not !Sys.interactive then
if not !Sys.interactive then
main ()</lang>
main ()</syntaxhighlight>


Invoked as a script:
Invoked as a script:


<lang sh>$ ocaml scriptedmain.ml
<syntaxhighlight lang="sh">$ ocaml scriptedmain.ml
Main: The meaning of life is 42</lang>
Main: The meaning of life is 42</syntaxhighlight>


Loaded into an ocaml toplevel/utop:
Loaded into an ocaml toplevel/utop:


<lang>$ ocaml
<syntaxhighlight lang="text">$ ocaml
...
...
# #use "scriptedmain.ml";;
# #use "scriptedmain.ml";;
Line 1,247: Line 1,247:
# meaning_of_life;;
# meaning_of_life;;
- : int = 42
- : int = 42
# </lang>
# </syntaxhighlight>


The limit of this technique is "avoiding running something when loading a script interactively". It's not applicable to other uses, like adding an example script to a file normally used as a library, as that code will also fire when users of the library are run.
The limit of this technique is "avoiding running something when loading a script interactively". It's not applicable to other uses, like adding an example script to a file normally used as a library, as that code will also fire when users of the library are run.
Line 1,256: Line 1,256:
meaningoflife.m
meaningoflife.m


<lang matlab>#!/usr/bin/env octave -qf
<syntaxhighlight lang="matlab">#!/usr/bin/env octave -qf


function y = meaningoflife()
function y = meaningoflife()
Line 1,266: Line 1,266:
endfunction
endfunction


main();</lang>
main();</syntaxhighlight>


test.m
test.m


<lang matlab>#!/usr/bin/env octave -qf
<syntaxhighlight lang="matlab">#!/usr/bin/env octave -qf


printf("Test: The meaning of life is %d", meaningoflife());</lang>
printf("Test: The meaning of life is %d", meaningoflife());</syntaxhighlight>


=={{header|Pascal}}==
=={{header|Pascal}}==
Line 1,278: Line 1,278:
Makefile:
Makefile:


<lang make>all: scriptedmain
<syntaxhighlight lang="make">all: scriptedmain


scriptedmain: scriptedmain.pas
scriptedmain: scriptedmain.pas
Line 1,290: Line 1,290:
-rm scriptedmain
-rm scriptedmain
-rm *.o
-rm *.o
-rm *.ppu</lang>
-rm *.ppu</syntaxhighlight>


scriptedmain.pas:
scriptedmain.pas:


<lang pascal>{$IFDEF scriptedmain}
<syntaxhighlight lang="pascal">{$IFDEF scriptedmain}
program ScriptedMain;
program ScriptedMain;
{$ELSE}
{$ELSE}
Line 1,311: Line 1,311:
writeln(MeaningOfLife())
writeln(MeaningOfLife())
{$ENDIF}
{$ENDIF}
end.</lang>
end.</syntaxhighlight>


test.pas:
test.pas:


<lang pascal>program Test;
<syntaxhighlight lang="pascal">program Test;
uses
uses
ScriptedMain;
ScriptedMain;
Line 1,321: Line 1,321:
write('Test: The meaning of life is: ');
write('Test: The meaning of life is: ');
writeln(MeaningOfLife())
writeln(MeaningOfLife())
end.</lang>
end.</syntaxhighlight>


Example:
Example:


<lang sh>$ make
<syntaxhighlight lang="sh">$ make
$ ./scriptedmain
$ ./scriptedmain
Main: The meaning of life is: 42
Main: The meaning of life is: 42
$ make test
$ make test
$ ./test
$ ./test
Test: The meaning of life is: 42</lang>
Test: The meaning of life is: 42</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
Perl has scripted main. The code inside <tt>unless(caller) { ... }</tt> only runs when <tt>Life.pm</tt> is the main program.
Perl has scripted main. The code inside <tt>unless(caller) { ... }</tt> only runs when <tt>Life.pm</tt> is the main program.


<lang perl>#!/usr/bin/env perl
<syntaxhighlight lang="perl">#!/usr/bin/env perl


# Life.pm
# Life.pm
Line 1,349: Line 1,349:
unless(caller) {
unless(caller) {
print "Main: The meaning of life is " . meaning_of_life() . "\n";
print "Main: The meaning of life is " . meaning_of_life() . "\n";
}</lang>
}</syntaxhighlight>


<lang perl>#!/usr/bin/env perl
<syntaxhighlight lang="perl">#!/usr/bin/env perl


# death.pl
# death.pl
Line 1,360: Line 1,360:


print "Life means " . Life::meaning_of_life . ".\n";
print "Life means " . Life::meaning_of_life . ".\n";
print "Death means invisible scary skeletons.\n";</lang>
print "Death means invisible scary skeletons.\n";</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
There is a builtin for this, which can even be asked to skip an arbitrary number of stack frames and that way find out exactly where it was effectively called from.
There is a builtin for this, which can even be asked to skip an arbitrary number of stack frames and that way find out exactly where it was effectively called from.
<!--<lang Phix>(notonline)-->
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (includefile)</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (includefile)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">mori</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">include_file</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"main"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"an include"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">mori</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">include_file</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"main"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"an include"</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|PHP}}==
=={{header|PHP}}==
Line 1,374: Line 1,374:
scriptedmain.php
scriptedmain.php


<lang php><?php
<syntaxhighlight lang="php"><?php
function meaning_of_life() {
function meaning_of_life() {
return 42;
return 42;
Line 1,386: Line 1,386:
main($argv);
main($argv);
}
}
?></lang>
?></syntaxhighlight>


test.php
test.php


<lang php><?php
<syntaxhighlight lang="php"><?php
require_once("scriptedmain.php");
require_once("scriptedmain.php");
echo "Test: The meaning of life is " . meaning_of_life() . "\n";
echo "Test: The meaning of life is " . meaning_of_life() . "\n";
?></lang>
?></syntaxhighlight>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
PicoLisp normally does it the other way round: It calls main from the command line with the '-' syntax if desired. Create an executable file (chmod +x) "life.l":
PicoLisp normally does it the other way round: It calls main from the command line with the '-' syntax if desired. Create an executable file (chmod +x) "life.l":
<lang PicoLisp>#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
<syntaxhighlight lang="picolisp">#!/usr/bin/picolisp /usr/lib/picolisp/lib.l


(de meaningOfLife ()
(de meaningOfLife ()
Line 1,404: Line 1,404:
(de lifemain ()
(de lifemain ()
(prinl "Main: The meaning of life is " (meaningOfLife))
(prinl "Main: The meaning of life is " (meaningOfLife))
(bye) )</lang>
(bye) )</syntaxhighlight>
and an executable file (chmod +x) "test.l":
and an executable file (chmod +x) "test.l":
<lang PicoLisp>#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
<syntaxhighlight lang="picolisp">#!/usr/bin/picolisp /usr/lib/picolisp/lib.l


(load "life.l")
(load "life.l")


(prinl "Test: The meaning of life is " (meaningOfLife))
(prinl "Test: The meaning of life is " (meaningOfLife))
(bye)</lang>
(bye)</syntaxhighlight>
Test:
Test:
<pre>$ ./life.l -lifemain
<pre>$ ./life.l -lifemain
Line 1,422: Line 1,422:
Python has scripted main.
Python has scripted main.


<lang python>#!/usr/bin/env python
<syntaxhighlight lang="python">#!/usr/bin/env python


# life.py
# life.py
Line 1,430: Line 1,430:


if __name__ == "__main__":
if __name__ == "__main__":
print("Main: The meaning of life is %s" % meaning_of_life())</lang>
print("Main: The meaning of life is %s" % meaning_of_life())</syntaxhighlight>


<lang python>#!/usr/bin/env python
<syntaxhighlight lang="python">#!/usr/bin/env python


# death.py
# death.py
Line 1,439: Line 1,439:


print("Life means %s." % meaning_of_life())
print("Life means %s." % meaning_of_life())
print("Death means invisible scary skeletons.")</lang>
print("Death means invisible scary skeletons.")</syntaxhighlight>


=={{header|R}}==
=={{header|R}}==
A way to check if code is running at "top level" is to check <code>length(sys.frames())</code>. This value will be zero for a file being run with <code>Rscript</code>, the <code>--file=</code> argument, or at the command line, and will be greater than 0 in all other conditions (such as package loading or code being sourced from another file.)
A way to check if code is running at "top level" is to check <code>length(sys.frames())</code>. This value will be zero for a file being run with <code>Rscript</code>, the <code>--file=</code> argument, or at the command line, and will be greater than 0 in all other conditions (such as package loading or code being sourced from another file.)


<lang R>#!/usr/bin/env Rscript
<syntaxhighlight lang="r">#!/usr/bin/env Rscript


meaningOfLife <- function() {
meaningOfLife <- function() {
Line 1,458: Line 1,458:
main(args)
main(args)
q("no")
q("no")
}</lang>
}</syntaxhighlight>


test.R
test.R


<lang R>#!/usr/bin/env Rscript
<syntaxhighlight lang="r">#!/usr/bin/env Rscript


source("scriptedmain.R")
source("scriptedmain.R")
Line 1,468: Line 1,468:
cat("Test: The meaning of life is", meaningOfLife(), "\n")
cat("Test: The meaning of life is", meaningOfLife(), "\n")


q("no")</lang>
q("no")</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
scriptedmain.rkt:
scriptedmain.rkt:
<lang racket>#!/usr/bin/env racket
<syntaxhighlight lang="racket">#!/usr/bin/env racket
#lang racket
#lang racket


Line 1,479: Line 1,479:
(define (meaning-of-life) 42)
(define (meaning-of-life) 42)


(module+ main (printf "Main: The meaning of life is ~a\n" (meaning-of-life)))</lang>
(module+ main (printf "Main: The meaning of life is ~a\n" (meaning-of-life)))</syntaxhighlight>


test.rkt:
test.rkt:
<lang racket>#!/usr/bin/env racket
<syntaxhighlight lang="racket">#!/usr/bin/env racket
#lang racket
#lang racket


(module+ main
(module+ main
(require "scriptedmain.rkt")
(require "scriptedmain.rkt")
(printf "Test: The meaning of life is ~a\n" (meaning-of-life)))</lang>
(printf "Test: The meaning of life is ~a\n" (meaning-of-life)))</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
Raku automatically calls MAIN on direct invocation, but this may be a multi dispatch, so a library may have multiple "scripted mains".
Raku automatically calls MAIN on direct invocation, but this may be a multi dispatch, so a library may have multiple "scripted mains".
<lang perl6>class LUE {
<syntaxhighlight lang="raku" line>class LUE {
has $.answer = 42;
has $.answer = 42;
}
}
Line 1,502: Line 1,502:
multi MAIN ('methods') {
multi MAIN ('methods') {
say ~LUE.^methods;
say ~LUE.^methods;
}</lang>
}</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program detects whether or not it is a "scripted main" program. */
<syntaxhighlight lang="rexx">/*REXX program detects whether or not it is a "scripted main" program. */
parse source . howInvoked @fn /*query REXX how this pgm got invoked. */
parse source . howInvoked @fn /*query REXX how this pgm got invoked. */


Line 1,522: Line 1,522:
/*────────────────────────────── The main code follows here ... ────────────────────────*/
/*────────────────────────────── The main code follows here ... ────────────────────────*/
say
say
say '(from' @fn"): and away we go ···"</lang> <br><br>
say '(from' @fn"): and away we go ···"</syntaxhighlight> <br><br>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Modulinos
# Project : Modulinos


Line 1,534: Line 1,534:
func main()
func main()
see "Main: The meaning of life is " + meaningoflife() + nl
see "Main: The meaning of life is " + meaningoflife() + nl
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 1,543: Line 1,543:
Ruby has scripted main.
Ruby has scripted main.


<lang ruby># life.rb
<syntaxhighlight lang="ruby"># life.rb


def meaning_of_life
def meaning_of_life
Line 1,551: Line 1,551:
if __FILE__ == $0
if __FILE__ == $0
puts "Main: The meaning of life is #{meaning_of_life}"
puts "Main: The meaning of life is #{meaning_of_life}"
end</lang>
end</syntaxhighlight>


<lang ruby># death.rb
<syntaxhighlight lang="ruby"># death.rb


require 'life'
require 'life'


puts "Life means #{meaning_of_life}."
puts "Life means #{meaning_of_life}."
puts "Death means invisible scary skeletons."</lang>
puts "Death means invisible scary skeletons."</syntaxhighlight>


=={{header|Rust}}==
=={{header|Rust}}==
Line 1,564: Line 1,564:


Makefile:
Makefile:
<lang make>all: scriptedmain
<syntaxhighlight lang="make">all: scriptedmain


scriptedmain: scriptedmain.rs
scriptedmain: scriptedmain.rs
Line 1,577: Line 1,577:
-rm -rf *.dylib
-rm -rf *.dylib
-rm scriptedmain
-rm scriptedmain
-rm -rf *.dSYM</lang>
-rm -rf *.dSYM</syntaxhighlight>


scriptedmain.rs:
scriptedmain.rs:
<lang rust>#[link(name = "scriptedmain")];
<syntaxhighlight lang="rust">#[link(name = "scriptedmain")];


use std;
use std;
Line 1,590: Line 1,590:
fn main() {
fn main() {
std::io::println("Main: The meaning of life is " + core::int::to_str(meaning_of_life(), 10u));
std::io::println("Main: The meaning of life is " + core::int::to_str(meaning_of_life(), 10u));
}</lang>
}</syntaxhighlight>


test.rs:
test.rs:
<lang rust>use scriptedmain;
<syntaxhighlight lang="rust">use scriptedmain;
use std;
use std;


fn main() {
fn main() {
std::io::println("Test: The meaning of life is " + core::int::to_str(scriptedmain::meaning_of_life(), 10u));
std::io::println("Test: The meaning of life is " + core::int::to_str(scriptedmain::meaning_of_life(), 10u));
}</lang>
}</syntaxhighlight>


Example:
Example:
<lang sh>$ make
<syntaxhighlight lang="sh">$ make
$ make test
$ make test
$ ./scriptedmain
$ ./scriptedmain
Main: The meaning of life is 42
Main: The meaning of life is 42
$ ./test
$ ./test
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


=={{header|SAC}}==
=={{header|SAC}}==
Makefile:
Makefile:
<lang make>all: scriptedmain
<syntaxhighlight lang="make">all: scriptedmain


scriptedmain: ScriptedMain.sac
scriptedmain: ScriptedMain.sac
Line 1,626: Line 1,626:
-rm libScriptedMainMod.a
-rm libScriptedMainMod.a
-rm scriptedmain
-rm scriptedmain
-rm scriptedmain.c</lang>
-rm scriptedmain.c</syntaxhighlight>


ScriptedMain.sac:
ScriptedMain.sac:
<lang c>#ifndef scriptedmain
<syntaxhighlight lang="c">#ifndef scriptedmain
module ScriptedMain;
module ScriptedMain;
#endif
#endif
Line 1,646: Line 1,646:
return(0);
return(0);
}
}
#endif</lang>
#endif</syntaxhighlight>


test.sac:
test.sac:
<lang c>use StdIO: all;
<syntaxhighlight lang="c">use StdIO: all;
use Array: all;
use Array: all;
use ScriptedMain: all;
use ScriptedMain: all;
Line 1,656: Line 1,656:
printf("Test: The meaning of life is %d\n", meaning_of_life());
printf("Test: The meaning of life is %d\n", meaning_of_life());
return(0);
return(0);
}</lang>
}</syntaxhighlight>


Example:
Example:
<lang sh>$ make
<syntaxhighlight lang="sh">$ make
$ make test
$ make test
$ ./scriptedmain
$ ./scriptedmain
Main: The meaning of life is 42
Main: The meaning of life is 42
$ ./test
$ ./test
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
Line 1,672: Line 1,672:
===Unix shell script===
===Unix shell script===
This code must be stored as a shell script.
This code must be stored as a shell script.
<lang bash>#!/bin/sh
<syntaxhighlight lang="bash">#!/bin/sh
exec scala "$0" "$@"
exec scala "$0" "$@"
!#
!#
Line 1,682: Line 1,682:
println(s"Use the routine to show that the hailstone sequence for the number: $nr.")
println(s"Use the routine to show that the hailstone sequence for the number: $nr.")
println(collatz.toList)
println(collatz.toList)
println(s"It has ${collatz.length} elements.")</lang>
println(s"It has ${collatz.length} elements.")</syntaxhighlight>


===Windows Command Script===
===Windows Command Script===
This code must be stored as a Windows Command Script e.g. Hailstone.cmd
This code must be stored as a Windows Command Script e.g. Hailstone.cmd
<lang winbatch>::#!
<syntaxhighlight lang="winbatch">::#!
@echo off
@echo off
call scala %0 %*
call scala %0 %*
Line 1,700: Line 1,700:
println(collatz.toList)
println(collatz.toList)
println(s"It has ${collatz.length} elements.")
println(s"It has ${collatz.length} elements.")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>C:\>Hailstone.cmd 42
<pre>C:\>Hailstone.cmd 42
Line 1,713: Line 1,713:
scriptedmain.scm
scriptedmain.scm


<lang scheme>#!/bin/sh
<syntaxhighlight lang="scheme">#!/bin/sh
#|
#|
exec csi -ss $0 ${1+"$@"}
exec csi -ss $0 ${1+"$@"}
Line 1,737: Line 1,737:


(if (equal? (car (program)) 'compiled)
(if (equal? (car (program)) 'compiled)
(main (cdr (argv))))</lang>
(main (cdr (argv))))</syntaxhighlight>


test.scm
test.scm


<lang scheme>#!/bin/sh
<syntaxhighlight lang="scheme">#!/bin/sh
#|
#|
exec csi -ss $0 ${1+"$@"}
exec csi -ss $0 ${1+"$@"}
Line 1,749: Line 1,749:
(load "scriptedmain.scm")
(load "scriptedmain.scm")
(display (format "Test: The meaning of life is ~a\n" (meaning-of-life)))
(display (format "Test: The meaning of life is ~a\n" (meaning-of-life)))
(exit))</lang>
(exit))</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby># Life.sm
<syntaxhighlight lang="ruby"># Life.sm


func meaning_of_life {
func meaning_of_life {
Line 1,760: Line 1,760:
if (__FILE__ == __MAIN__) {
if (__FILE__ == __MAIN__) {
say "Main: The meaning of life is #{meaning_of_life()}"
say "Main: The meaning of life is #{meaning_of_life()}"
}</lang>
}</syntaxhighlight>


<lang ruby># test.sf
<syntaxhighlight lang="ruby"># test.sf


include Life
include Life


say "Test: The meaning of life is #{Life::meaning_of_life()}."</lang>
say "Test: The meaning of life is #{Life::meaning_of_life()}."</syntaxhighlight>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
Line 1,774: Line 1,774:
Example
Example


<lang shell>$ gst-package -t ~/.st package.xml &>/dev/null
<syntaxhighlight lang="shell">$ gst-package -t ~/.st package.xml &>/dev/null


$ ./scriptedmain.st
$ ./scriptedmain.st
Line 1,780: Line 1,780:


$ ./test.st
$ ./test.st
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


package.xml
package.xml


<lang xml><packages>
<syntaxhighlight lang="xml"><packages>
<package>
<package>
<name>ScriptedMain</name>
<name>ScriptedMain</name>
Line 1,790: Line 1,790:
<file>scriptedmain.st</file>
<file>scriptedmain.st</file>
</package>
</package>
</packages></lang>
</packages></syntaxhighlight>


scriptedmain.st
scriptedmain.st


<lang smalltalk>"exec" "gst" "-f" "$0" "$0" "$@"
<syntaxhighlight lang="smalltalk">"exec" "gst" "-f" "$0" "$0" "$@"
"exit"
"exit"


Line 1,809: Line 1,809:
(((Smalltalk getArgc) > 0) and: [ ((Smalltalk getArgv: 1) endsWith: 'scriptedmain.st') ]) ifTrue: [
(((Smalltalk getArgc) > 0) and: [ ((Smalltalk getArgv: 1) endsWith: 'scriptedmain.st') ]) ifTrue: [
main value.
main value.
].</lang>
].</syntaxhighlight>


test.st
test.st


<lang smalltalk>"exec" "gst" "-f" "$0" "$0" "$@"
<syntaxhighlight lang="smalltalk">"exec" "gst" "-f" "$0" "$0" "$@"
"exit"
"exit"


Line 1,819: Line 1,819:
PackageLoader fileInPackage: 'ScriptedMain'.
PackageLoader fileInPackage: 'ScriptedMain'.


Transcript show: 'Test: The meaning of life is ', ((ScriptedMain meaningOfLife) printString); cr.</lang>
Transcript show: 'Test: The meaning of life is ', ((ScriptedMain meaningOfLife) printString); cr.</syntaxhighlight>


=={{header|Swift}}==
=={{header|Swift}}==
Line 1,827: Line 1,827:
Example
Example


<lang shell>$ make
<syntaxhighlight lang="shell">$ make
mkdir -p bin/
mkdir -p bin/
swiftc -D SCRIPTEDMAIN -o bin/ScriptedMain ScriptedMain.swift
swiftc -D SCRIPTEDMAIN -o bin/ScriptedMain ScriptedMain.swift
Line 1,836: Line 1,836:
Main: The meaning of life is 42
Main: The meaning of life is 42
bin/Test
bin/Test
Test: The meaning of life is 42</lang>
Test: The meaning of life is 42</syntaxhighlight>


Makefile
Makefile


<lang make>all: bin/ScriptedMain bin/Test
<syntaxhighlight lang="make">all: bin/ScriptedMain bin/Test
bin/ScriptedMain
bin/ScriptedMain
bin/Test
bin/Test
Line 1,860: Line 1,860:
-rm *.swiftdoc
-rm *.swiftdoc
-rm *.dylib
-rm *.dylib
</syntaxhighlight>
</lang>


ScriptedMain.swift
ScriptedMain.swift


<lang swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


public class ScriptedMain {
public class ScriptedMain {
Line 1,885: Line 1,885:
}
}
#endif
#endif
</syntaxhighlight>
</lang>


Test.swift
Test.swift


<lang swift>import Foundation
<syntaxhighlight lang="swift">import Foundation
import ScriptedMain
import ScriptedMain


Line 1,907: Line 1,907:
}
}
#endif
#endif
</syntaxhighlight>
</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>proc main {args} {
<syntaxhighlight lang="tcl">proc main {args} {
puts "Directory: [pwd]"
puts "Directory: [pwd]"
puts "Program: $::argv0"
puts "Program: $::argv0"
Line 1,919: Line 1,919:
if {$::argv0 eq [info script]} {
if {$::argv0 eq [info script]} {
main {*}$::argv
main {*}$::argv
}</lang>
}</syntaxhighlight>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
Line 1,926: Line 1,926:
scriptedmain.sh
scriptedmain.sh


<lang sh>#!/usr/bin/env sh
<syntaxhighlight lang="sh">#!/usr/bin/env sh


meaning_of_life() {
meaning_of_life() {
Line 1,940: Line 1,940:
then
then
main
main
fi</lang>
fi</syntaxhighlight>


test.sh
test.sh


<lang sh>#!/bin/bash
<syntaxhighlight lang="sh">#!/bin/bash


path=$(dirname -- "$0")
path=$(dirname -- "$0")
Line 1,951: Line 1,951:
meaning_of_life
meaning_of_life
echo "Test: The meaning of life is $?"
echo "Test: The meaning of life is $?"
</syntaxhighlight>
</lang>


=={{header|Wren}}==
=={{header|Wren}}==
Line 1,959: Line 1,959:


First we create a module for our modulino:
First we create a module for our modulino:
<lang ecmascript>/* modulino.wren */
<syntaxhighlight lang="ecmascript">/* modulino.wren */


var MeaningOfLife = Fn.new { 42 }
var MeaningOfLife = Fn.new { 42 }
Line 1,971: Line 1,971:
if (Process.allArguments[1] == "modulino.wren") { // if true, not a library
if (Process.allArguments[1] == "modulino.wren") { // if true, not a library
main.call()
main.call()
}</lang>
}</syntaxhighlight>


and run it to make sure it works OK when run directly:
and run it to make sure it works OK when run directly:
Line 1,980: Line 1,980:


Next we create another module which imports the modulino:
Next we create another module which imports the modulino:
<lang ecmascript>/* modulino_main.wren */
<syntaxhighlight lang="ecmascript">/* modulino_main.wren */


import "/modulino" for MeaningOfLife
import "/modulino" for MeaningOfLife
Line 1,988: Line 1,988:
}
}


main.call()</lang>
main.call()</syntaxhighlight>


and run this to make sure the modulino's ''main()'' function doesn't run:
and run this to make sure the modulino's ''main()'' function doesn't run:
Line 2,000: Line 2,000:
On the ZX Spectrum, there is no main function as such, however a saved program can be made to start running from a particular line number by providing the line number as a parameter to save command. If the program is being merged as a module, then it does not run automatically. The following example will save the program in memory so that it starts running from line 500:
On the ZX Spectrum, there is no main function as such, however a saved program can be made to start running from a particular line number by providing the line number as a parameter to save command. If the program is being merged as a module, then it does not run automatically. The following example will save the program in memory so that it starts running from line 500:


<lang zxbasic>SAVE "MYPROG" LINE 500: REM For a program with main code starting at line 500</lang>
<syntaxhighlight lang="zxbasic">SAVE "MYPROG" LINE 500: REM For a program with main code starting at line 500</syntaxhighlight>


{{omit from|Ada}}
{{omit from|Ada}}