Modulinos: Difference between revisions
Content added Content deleted
m (note security holes around empty argv) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 14: | Line 14: | ||
=={{header|11l}}== |
=={{header|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())</ |
print(‘Main: The meaning of life is ’meaning_of_life())</syntaxhighlight> |
||
< |
<syntaxhighlight lang="11l">// death.11l |
||
print(‘Life means ’life:meaning_of_life()‘.’) |
print(‘Life means ’life:meaning_of_life()‘.’) |
||
print(‘Death means nothing.’)</ |
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: |
||
< |
<syntaxhighlight lang="applescript">display dialog "Hello"</syntaxhighlight> |
||
or |
or |
||
< |
<syntaxhighlight lang="applescript">on run |
||
display dialog "Hello" |
display dialog "Hello" |
||
end run</ |
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: |
||
< |
<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</ |
end run</syntaxhighlight> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
Line 51: | Line 51: | ||
===Library=== |
===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|"</ |
print ~"Library: The meaning of life is |meaningOfLife|"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 66: | Line 66: | ||
===Main=== |
===Main=== |
||
< |
<syntaxhighlight lang="rebol">do.import relative "modulinos - library.art" |
||
print ~"Life means |meaningOfLife|." |
print ~"Life means |meaningOfLife|." |
||
print "Death means invisible scary skeletons."</ |
print "Death means invisible scary skeletons."</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 84: | Line 84: | ||
Example |
Example |
||
< |
<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</ |
Test: The meaning of life is</syntaxhighlight> |
||
Makefile |
Makefile |
||
< |
<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</ |
-rm test.exe</syntaxhighlight> |
||
scriptedmain.h |
scriptedmain.h |
||
<lang |
<syntaxhighlight lang="c">int meaning_of_life();</syntaxhighlight> |
||
scriptedmain.c |
scriptedmain.c |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
int meaning_of_life() { |
int meaning_of_life() { |
||
Line 128: | Line 128: | ||
} |
} |
||
#endif</ |
#endif</syntaxhighlight> |
||
test.c |
test.c |
||
< |
<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; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 147: | Line 147: | ||
Example |
Example |
||
< |
<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</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
Makefile |
Makefile |
||
< |
<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</ |
-rm test.exe</syntaxhighlight> |
||
scriptedmain.h |
scriptedmain.h |
||
<lang |
<syntaxhighlight lang="cpp">int meaning_of_life();</syntaxhighlight> |
||
scriptedmain.cpp |
scriptedmain.cpp |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
using namespace std; |
using namespace std; |
||
Line 192: | Line 192: | ||
} |
} |
||
#endif</ |
#endif</syntaxhighlight> |
||
test.cpp |
test.cpp |
||
< |
<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; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 212: | Line 212: | ||
scriptedmain.clj: |
scriptedmain.clj: |
||
< |
<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*)))</ |
(apply -main (rest *command-line-args*)))</syntaxhighlight> |
||
test.clj: |
test.clj: |
||
< |
<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*)))</ |
(apply -main (rest *command-line-args*)))</syntaxhighlight> |
||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
scriptedmain.coffee: |
scriptedmain.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()</ |
if not module.parent then main()</syntaxhighlight> |
||
test.coffee: |
test.coffee: |
||
< |
<syntaxhighlight lang="coffeescript">#!/usr/bin/env coffee |
||
sm = require "./scriptedmain" |
sm = require "./scriptedmain" |
||
console.log "Test: The meaning of life is " + sm.meaningOfLife()</ |
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 |
||
< |
<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))</ |
nil))</syntaxhighlight> |
||
scriptedmain.lisp |
scriptedmain.lisp |
||
< |
<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)))</ |
(main args)))</syntaxhighlight> |
||
test.lisp |
test.lisp |
||
< |
<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))</ |
(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: |
||
< |
<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()); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
test.d: |
test.d: |
||
< |
<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()); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<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</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
=={{header|Dart}}== |
=={{header|Dart}}== |
||
scriptedmain.dart: |
scriptedmain.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()}"); |
||
}</ |
}</syntaxhighlight> |
||
test.dart: |
test.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()}"); |
||
}</ |
}</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<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</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
Line 402: | Line 402: | ||
scriptedmain.el |
scriptedmain.el |
||
< |
<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)))</ |
(message "Main: The meaning of life is %d" (meaning-of-life)))</syntaxhighlight> |
||
test.el |
test.el |
||
< |
<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)))</ |
(message "Test: The meaning of life is %d" (meaning-of-life)))</syntaxhighlight> |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
Line 428: | Line 428: | ||
Makefile: |
Makefile: |
||
< |
<syntaxhighlight lang="make">all: t |
||
t: scriptedmain.beam test.beam |
t: scriptedmain.beam test.beam |
||
Line 441: | Line 441: | ||
clean: |
clean: |
||
-rm *.beam</ |
-rm *.beam</syntaxhighlight> |
||
scriptedmain.erl: |
scriptedmain.erl: |
||
< |
<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().</ |
init:stop().</syntaxhighlight> |
||
test.erl: |
test.erl: |
||
< |
<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().</ |
init:stop().</syntaxhighlight> |
||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
Line 474: | Line 474: | ||
Example: |
Example: |
||
< |
<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</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
Makefile: |
Makefile: |
||
< |
<syntaxhighlight lang="make">all: scriptedmain.exe test.exe |
||
scriptedmain.exe: ScriptedMain.fs |
scriptedmain.exe: ScriptedMain.fs |
||
Line 493: | Line 493: | ||
clean: |
clean: |
||
-rm *.exe</ |
-rm *.exe</syntaxhighlight> |
||
ScriptedMain.fs: |
ScriptedMain.fs: |
||
< |
<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</ |
printfn "Main: The meaning of life is %d" meaningOfLife</syntaxhighlight> |
||
Test.fs: |
Test.fs: |
||
< |
<syntaxhighlight lang="fsharp">module Test = |
||
open ScriptedMain |
open ScriptedMain |
||
let main = |
let main = |
||
printfn "Test: The meaning of life is %d" ScriptedMain.meaningOfLife</ |
printfn "Test: The meaning of life is %d" ScriptedMain.meaningOfLife</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 518: | Line 518: | ||
Example: |
Example: |
||
< |
<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</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
~/.factor-rc: |
~/.factor-rc: |
||
< |
<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 ;</ |
SYNTAX: INCLUDING: ";" [ include-vocab ] each-token ;</syntaxhighlight> |
||
scriptedmain.factor: |
scriptedmain.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</ |
MAIN: main</syntaxhighlight> |
||
test.factor: |
test.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</ |
MAIN: main</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Line 563: | Line 563: | ||
Given this awful running reference: |
Given this awful running reference: |
||
< |
<syntaxhighlight lang="forth">42 constant Douglas-Adams |
||
: go ( -- ) |
: go ( -- ) |
||
." The meaning of life is " Douglas-Adams . cr ;</ |
." 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}} |
||
< |
<syntaxhighlight lang="forth">' go 'MAIN ! |
||
program douglas-adams</ |
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}} |
||
< |
<syntaxhighlight lang="forth">#! /usr/bin/env gforth |
||
42 constant Douglas-Adams |
42 constant Douglas-Adams |
||
.( The meaning of life is ) Douglas-Adams . cr bye</ |
.( 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}} |
||
< |
<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.)</ |
cr .( Why aren't you running this as a script? It only provides a constant.)</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|Ring}} |
{{trans|Ring}} |
||
< |
<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: |
||
< |
<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()) |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="go">// modulino_main.go |
||
package main |
package main |
||
func main() { |
func main() { |
||
libMain() |
libMain() |
||
}</ |
}</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: |
||
< |
<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()) |
||
}</ |
}</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: |
||
< |
<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</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
ScriptedMain.groovy: |
ScriptedMain.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 |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Test.groovy: |
Test.groovy: |
||
< |
<syntaxhighlight lang="groovy">#!/usr/bin/env groovy |
||
println "Test: The meaning of life is " + ScriptedMain.meaningOfLife</ |
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. |
||
< |
<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</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
scriptedmain.hs |
scriptedmain.hs |
||
< |
<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</ |
main = putStrLn $ "Main: The meaning of life is " ++ show meaningOfLife</syntaxhighlight> |
||
test.hs |
test.hs |
||
< |
<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</ |
main = putStrLn $ "Test: The meaning of life is " ++ show meaningOfLife</syntaxhighlight> |
||
=={{header|Io}}== |
=={{header|Io}}== |
||
Line 749: | Line 749: | ||
ScriptedMain.io: |
ScriptedMain.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 |
||
)</ |
)</syntaxhighlight> |
||
test.io: |
test.io: |
||
< |
<syntaxhighlight lang="io">#!/usr/bin/env io |
||
"Test: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println</ |
"Test: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println</syntaxhighlight> |
||
< |
<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</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Line 773: | Line 773: | ||
modulinos.ijs: |
modulinos.ijs: |
||
< |
<syntaxhighlight lang="j">#!/usr/bin/env ijconsole |
||
meaningOfLife =: 42 |
meaningOfLife =: 42 |
||
Line 788: | Line 788: | ||
) |
) |
||
shouldrun 0</ |
shouldrun 0</syntaxhighlight> |
||
test.j: |
test.j: |
||
< |
<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 ''</ |
exit ''</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<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</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 812: | Line 812: | ||
ScriptedMain.java |
ScriptedMain.java |
||
< |
<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()); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Test.java |
Test.java |
||
< |
<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()); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Line 835: | Line 835: | ||
scriptedmain.js |
scriptedmain.js |
||
< |
<syntaxhighlight lang="javascript">#!/usr/bin/env node |
||
function meaningOfLife() { return 42; } |
function meaningOfLife() { return 42; } |
||
Line 845: | Line 845: | ||
} |
} |
||
if (!module.parent) { main(); }</ |
if (!module.parent) { main(); }</syntaxhighlight> |
||
test.js |
test.js |
||
< |
<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());</ |
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: |
||
< |
<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: |
||
< |
<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. |
||
< |
<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</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
Makefile |
Makefile |
||
< |
<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</ |
-rm scriptedmain.bc</syntaxhighlight> |
||
scriptedmain.ll |
scriptedmain.ll |
||
< |
<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 |
||
}</ |
}</syntaxhighlight> |
||
test.ll |
test.ll |
||
< |
<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 |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 988: | Line 988: | ||
scriptedmain.lua |
scriptedmain.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</ |
end</syntaxhighlight> |
||
test.lua |
test.lua |
||
< |
<syntaxhighlight lang="lua">#!/usr/bin/env lua |
||
sm = require("scriptedmain") |
sm = require("scriptedmain") |
||
print("Test: The meaning of life is " .. sm.meaningoflife())</ |
print("Test: The meaning of life is " .. sm.meaningoflife())</syntaxhighlight> |
||
=={{header|Make}}== |
=={{header|Make}}== |
||
Example |
Example |
||
< |
<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)</ |
(Test)</syntaxhighlight> |
||
scriptedmain.mf |
scriptedmain.mf |
||
< |
<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 |
||
< |
<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 |
||
< |
<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[]]] |
||
]</ |
]</syntaxhighlight> |
||
test.ma: |
test.ma: |
||
< |
<syntaxhighlight lang="mathematica">#!/usr/bin/env MathKernel -script |
||
Get["scriptedmain.ma"] |
Get["scriptedmain.ma"] |
||
Print["Test: The meaning of life is " <> ToString[MeaningOfLife[]]]</ |
Print["Test: The meaning of life is " <> ToString[MeaningOfLife[]]]</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<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</ |
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: |
||
< |
<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: |
||
< |
<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: |
||
< |
<syntaxhighlight lang="oz">functor |
||
import |
import |
||
ScriptedMain |
ScriptedMain |
||
Line 1,133: | Line 1,133: | ||
end |
end |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
=={{header|newLISP}}== |
=={{header|newLISP}}== |
||
Line 1,140: | Line 1,140: | ||
scriptedmain.lsp |
scriptedmain.lsp |
||
< |
<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)</ |
(context MAIN)</syntaxhighlight> |
||
test.lsp |
test.lsp |
||
< |
<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)</ |
(exit)</syntaxhighlight> |
||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
Line 1,166: | Line 1,166: | ||
scriptedmain.h: |
scriptedmain.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</ |
@end</syntaxhighlight> |
||
scriptedmain.m: |
scriptedmain.m: |
||
< |
<syntaxhighlight lang="objc">#import "scriptedmain.h" |
||
#import <Foundation/Foundation.h> |
#import <Foundation/Foundation.h> |
||
Line 1,195: | Line 1,195: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
test.m: |
test.m: |
||
< |
<syntaxhighlight lang="objc">#import "scriptedmain.h" |
||
#import <Foundation/Foundation.h> |
#import <Foundation/Foundation.h> |
||
Line 1,210: | Line 1,210: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
< |
<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</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
Line 1,223: | Line 1,223: | ||
scriptedmain.ml |
scriptedmain.ml |
||
< |
<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 ()</ |
main ()</syntaxhighlight> |
||
Invoked as a script: |
Invoked as a script: |
||
< |
<syntaxhighlight lang="sh">$ ocaml scriptedmain.ml |
||
Main: The meaning of life is 42</ |
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 |
||
# </ |
# </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 |
||
< |
<syntaxhighlight lang="matlab">#!/usr/bin/env octave -qf |
||
function y = meaningoflife() |
function y = meaningoflife() |
||
Line 1,266: | Line 1,266: | ||
endfunction |
endfunction |
||
main();</ |
main();</syntaxhighlight> |
||
test.m |
test.m |
||
< |
<syntaxhighlight lang="matlab">#!/usr/bin/env octave -qf |
||
printf("Test: The meaning of life is %d", meaningoflife());</ |
printf("Test: The meaning of life is %d", meaningoflife());</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 1,278: | Line 1,278: | ||
Makefile: |
Makefile: |
||
< |
<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</ |
-rm *.ppu</syntaxhighlight> |
||
scriptedmain.pas: |
scriptedmain.pas: |
||
< |
<syntaxhighlight lang="pascal">{$IFDEF scriptedmain} |
||
program ScriptedMain; |
program ScriptedMain; |
||
{$ELSE} |
{$ELSE} |
||
Line 1,311: | Line 1,311: | ||
writeln(MeaningOfLife()) |
writeln(MeaningOfLife()) |
||
{$ENDIF} |
{$ENDIF} |
||
end.</ |
end.</syntaxhighlight> |
||
test.pas: |
test.pas: |
||
< |
<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.</ |
end.</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<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</ |
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. |
||
< |
<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"; |
||
}</ |
}</syntaxhighlight> |
||
< |
<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";</ |
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. |
||
<!--< |
<!--<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
Line 1,374: | Line 1,374: | ||
scriptedmain.php |
scriptedmain.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); |
||
} |
} |
||
?></ |
?></syntaxhighlight> |
||
test.php |
test.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"; |
||
?></ |
?></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": |
||
< |
<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) )</ |
(bye) )</syntaxhighlight> |
||
and an executable file (chmod +x) "test.l": |
and an executable file (chmod +x) "test.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)</ |
(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. |
||
< |
<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())</ |
print("Main: The meaning of life is %s" % meaning_of_life())</syntaxhighlight> |
||
< |
<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.")</ |
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.) |
||
< |
<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") |
||
}</ |
}</syntaxhighlight> |
||
test.R |
test.R |
||
< |
<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")</ |
q("no")</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
scriptedmain.rkt: |
scriptedmain.rkt: |
||
< |
<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)))</ |
(module+ main (printf "Main: The meaning of life is ~a\n" (meaning-of-life)))</syntaxhighlight> |
||
test.rkt: |
test.rkt: |
||
< |
<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)))</ |
(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 |
<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; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<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 ···"</ |
say '(from' @fn"): and away we go ···"</syntaxhighlight> <br><br> |
||
=={{header|Ring}}== |
=={{header|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. |
||
< |
<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</ |
end</syntaxhighlight> |
||
< |
<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."</ |
puts "Death means invisible scary skeletons."</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 1,564: | Line 1,564: | ||
Makefile: |
Makefile: |
||
< |
<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</ |
-rm -rf *.dSYM</syntaxhighlight> |
||
scriptedmain.rs: |
scriptedmain.rs: |
||
< |
<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)); |
||
}</ |
}</syntaxhighlight> |
||
test.rs: |
test.rs: |
||
< |
<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)); |
||
}</ |
}</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<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</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
=={{header|SAC}}== |
=={{header|SAC}}== |
||
Makefile: |
Makefile: |
||
< |
<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</ |
-rm scriptedmain.c</syntaxhighlight> |
||
ScriptedMain.sac: |
ScriptedMain.sac: |
||
< |
<syntaxhighlight lang="c">#ifndef scriptedmain |
||
module ScriptedMain; |
module ScriptedMain; |
||
#endif |
#endif |
||
Line 1,646: | Line 1,646: | ||
return(0); |
return(0); |
||
} |
} |
||
#endif</ |
#endif</syntaxhighlight> |
||
test.sac: |
test.sac: |
||
< |
<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); |
||
}</ |
}</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<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</ |
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. |
||
< |
<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.")</ |
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 |
||
< |
<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 |
||
< |
<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))))</ |
(main (cdr (argv))))</syntaxhighlight> |
||
test.scm |
test.scm |
||
< |
<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))</ |
(exit))</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<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()}" |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="ruby"># test.sf |
||
include Life |
include Life |
||
say "Test: The meaning of life is #{Life::meaning_of_life()}."</ |
say "Test: The meaning of life is #{Life::meaning_of_life()}."</syntaxhighlight> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
Line 1,774: | Line 1,774: | ||
Example |
Example |
||
< |
<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</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
package.xml |
package.xml |
||
< |
<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></ |
</packages></syntaxhighlight> |
||
scriptedmain.st |
scriptedmain.st |
||
< |
<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. |
||
].</ |
].</syntaxhighlight> |
||
test.st |
test.st |
||
< |
<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.</ |
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 |
||
< |
<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</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
Makefile |
Makefile |
||
< |
<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 |
||
< |
<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 |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
import ScriptedMain |
import ScriptedMain |
||
Line 1,907: | Line 1,907: | ||
} |
} |
||
#endif |
#endif |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<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 |
||
}</ |
}</syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 1,926: | Line 1,926: | ||
scriptedmain.sh |
scriptedmain.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</ |
fi</syntaxhighlight> |
||
test.sh |
test.sh |
||
< |
<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: |
||
< |
<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() |
||
}</ |
}</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: |
||
< |
<syntaxhighlight lang="ecmascript">/* modulino_main.wren */ |
||
import "/modulino" for MeaningOfLife |
import "/modulino" for MeaningOfLife |
||
Line 1,988: | Line 1,988: | ||
} |
} |
||
main.call()</ |
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: |
||
< |
<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}} |