Modulinos: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|Wren}}: Minor tidy) |
|||
(18 intermediate revisions by 11 users not shown) | |||
Line 5:
Sometimes getting the [[ScriptName]] is required in order to determine when to run main().
Care when manipulating command line arguments, due to subtle exec security constraints that may or not be enforced on implicit argv[0]. https://ryiron.wordpress.com/2013/12/16/argv-silliness/
: ''This is still a draft task, and the current task description has caused mega confusion. See '''[[Talk:Modulinos]]''' for numerous attempts to understand the task and to rewrite the task description.''
Line 10 ⟶ 12:
: '''The task [[Executable library]] is written to replace this task.''' ''This task's future is in doubt as its aims are not clear enough.''
<br><br>
=={{header|11l}}==
<syntaxhighlight lang="11l">// life.11l
F meaning_of_life()
R ‘*’.code
:start:
print(‘Main: The meaning of life is ’meaning_of_life())</syntaxhighlight>
<syntaxhighlight lang="11l">// death.11l
print(‘Life means ’life:meaning_of_life()‘.’)
print(‘Death means nothing.’)</syntaxhighlight>
=={{header|AppleScript}}==
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
<syntaxhighlight lang="applescript">on run
display dialog "Hello"
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:
<syntaxhighlight lang="applescript">on run
if ((path to me) = (path to current application)) then
display dialog "I'm running in my own application."
else
display dialog "I'm being run from another script or application."
end if
end run</syntaxhighlight>
=={{header|Arturo}}==
===Library===
<syntaxhighlight lang="rebol">; modulinos - library
meaningOfLife: function [][
42
]
if standalone? ->
print ~"Library: The meaning of life is |meaningOfLife|"</syntaxhighlight>
{{out}}
<pre>Library: The meaning of life is 42</pre>
===Main===
<syntaxhighlight lang="rebol">do.import relative "modulinos - library.art"
print ~"Life means |meaningOfLife|."
print "Death means invisible scary skeletons."</syntaxhighlight>
{{out}}
<pre>Life means 42.
Death means invisible scary skeletons.</pre>
=={{header|C}}==
Line 19 ⟶ 84:
Example
<
./scriptedmain
Main: The meaning of life is 42
./test
Test: The meaning of life is</
Makefile
<
./scriptedmain
./test
Line 41 ⟶ 106:
-rm test
-rm scriptedmain.exe
-rm test.exe</
scriptedmain.h
<syntaxhighlight lang
scriptedmain.c
<
int meaning_of_life() {
Line 63 ⟶ 128:
}
#endif</
test.c
<
#include <stdio.h>
Line 75 ⟶ 140:
printf("Test: The meaning of life is %d\n", meaning_of_life());
return 0;
}</
=={{header|C++}}==
Line 82 ⟶ 147:
Example
<
./scriptedmain
Main: The meaning of life is 42
./test
Test: The meaning of life is 42</
Makefile
<
./scriptedmain
./test
Line 104 ⟶ 169:
-rm test
-rm scriptedmain.exe
-rm test.exe</
scriptedmain.h
<syntaxhighlight lang
scriptedmain.cpp
<
using namespace std;
Line 127 ⟶ 192:
}
#endif</
test.cpp
<
#include <iostream>
Line 141 ⟶ 206:
cout << "Test: The meaning of life is " << meaning_of_life() << endl;
return 0;
}</
=={{header|Clojure}}==
Line 147 ⟶ 212:
scriptedmain.clj:
<
":";exit
Line 159 ⟶ 224:
(when (.contains (first *command-line-args*) *source-path*)
(apply -main (rest *command-line-args*)))</
test.clj:
<
":";exit
Line 174 ⟶ 239:
(when (.contains (first *command-line-args*) *source-path*)
(apply -main (rest *command-line-args*)))</
=={{header|CoffeeScript}}==
scriptedmain.coffee:
<
meaningOfLife = () -> 42
Line 187 ⟶ 252:
console.log "Main: The meaning of life is " + meaningOfLife()
if not module.parent then main()</
test.coffee:
<
sm = require "./scriptedmain"
console.log "Test: The meaning of life is " + sm.meaningOfLife()</
=={{header|Common Lisp}}==
Line 203 ⟶ 268:
~/.clisprc.lisp
<
(set-dispatch-macro-character #\# #\!
(lambda (stream character n)
(declare (ignore character n))
(read-line stream nil nil t)
nil))</
scriptedmain.lisp
<
#|
exec clisp -q -q $0 $0 ${1+"$@"}
Line 242 ⟶ 307:
args
:test #'(lambda (x y) (search x y :test #'equalp)))
(main args)))</
test.lisp
<
#|
exec clisp -q -q $0 $0 ${1+"$@"}
Line 253 ⟶ 318:
(load "scriptedmain.lisp")
(format t "Test: The meaning of life is ~a~%" (meaning-of-life))</
=={{header|D}}==
Line 261 ⟶ 326:
scriptedmain.d:
<
module scriptedmain;
Line 275 ⟶ 340:
writeln("Main: The meaning of life is ", meaningOfLife());
}
}</
test.d:
<
import scriptedmain;
Line 288 ⟶ 353:
writeln("Test: The meaning of life is ", meaningOfLife());
}
}</
Example:
<
Main: The meaning of life is 42
$ ./test.d
Line 301 ⟶ 366:
$ dmd test.d scriptedmain.d -version=test
$ ./test
Test: The meaning of life is 42</
=={{header|Dart}}==
scriptedmain.dart:
<
#library("scriptedmain");
Line 315 ⟶ 380:
main() {
print("Main: The meaning of life is ${meaningOfLife()}");
}</
test.dart:
<
#import("scriptedmain.dart", prefix: "scriptedmain");
Line 324 ⟶ 389:
main() {
print("Test: The meaning of life is ${scriptedmain.meaningOfLife()}");
}</
Example:
<
Main: The meaning of life is 42
$ ./test.dart
Test: The meaning of life is 42</
=={{header|Emacs Lisp}}==
Line 337 ⟶ 402:
scriptedmain.el
<
;;; Shebang from John Swaby
Line 345 ⟶ 410:
(defun main ()
(message "Main: The meaning of life is %d" (meaning-of-life)))</
test.el
<
;;; Shebang from John Swaby
Line 357 ⟶ 422:
(setq load-path (cons default-directory load-path))
(load "scriptedmain.el" nil t)
(message "Test: The meaning of life is %d" (meaning-of-life)))</
=={{header|EMal}}==
{{trans|Wren}}
<syntaxhighlight lang="emal">
^|We have created a module named ModulinosPart.emal.
|^
in Org:RosettaCode
type ModulinosPart
fun meaningOfLife = int by block do return 42 end
fun main = void by block do writeLine("The meaning of life is " + meaningOfLife() + ".") end
if Runtime.direct() do main() end
</syntaxhighlight>
{{out}}
<pre>
emal.exe Org\RosettaCode\ModulinosPart.emal
The meaning of life is 42.
</pre>
<syntaxhighlight lang="emal">
^|Then we create a new module named Modulinos.emal,
|this imports the previous module.
|^
in Org:RosettaCode
load :ModulinosPart
type Modulinos
fun main = int by List args
writeLine("Who says the meaning of life is " + ModulinosPart.meaningOfLife() + "?")
return 0
end
exit main(Runtime.args)
</syntaxhighlight>
{{out}}
<pre>
emal.exe Org\RosettaCode\Modulinos.emal
Who says the meaning of life is 42?
</pre>
=={{header|Erlang}}==
Line 363 ⟶ 463:
Makefile:
<
t: scriptedmain.beam test.beam
Line 376 ⟶ 476:
clean:
-rm *.beam</
scriptedmain.erl:
<
-export([meaning_of_life/0, start/0]).
Line 386 ⟶ 486:
start() ->
io:format("Main: The meaning of life is ~w~n", [meaning_of_life()]),
init:stop().</
test.erl:
<
-export([start/0]).
-import(scriptedmain, [meaning_of_life/0]).
Line 395 ⟶ 495:
start() ->
io:format("Test: The meaning of life is ~w~n", [meaning_of_life()]),
init:stop().</
=={{header|F Sharp|F#}}==
Line 409 ⟶ 509:
Example:
<
fsharpc --out:scriptedmain.exe ScriptedMain.fs
fsharpc --out:test.exe ScriptedMain.fs Test.fs
Line 415 ⟶ 515:
Main: The meaning of life is 42
$ mono test.exe
Test: The meaning of life is 42</
Makefile:
<
scriptedmain.exe: ScriptedMain.fs
Line 428 ⟶ 528:
clean:
-rm *.exe</
ScriptedMain.fs:
<
module ScriptedMain =
Line 438 ⟶ 538:
let main =
printfn "Main: The meaning of life is %d" meaningOfLife</
Test.fs:
<
open ScriptedMain
let main =
printfn "Test: The meaning of life is %d" ScriptedMain.meaningOfLife</
=={{header|Factor}}==
Line 453 ⟶ 553:
Example:
<
Main: The meaning of life is 42
$ ./test.factor
Test: The meaning of life is 42</
~/.factor-rc:
<
USING: kernel vocabs.loader parser sequences lexer vocabs.parser ;
Line 467 ⟶ 567:
: include-vocab ( vocab -- ) dup ".factor" append parse-file append use-vocab ;
SYNTAX: INCLUDING: ";" [ include-vocab ] each-token ;</
scriptedmain.factor:
<
USING: io math.parser ;
Line 480 ⟶ 580:
: main ( -- ) meaning-of-life "Main: The meaning of life is " write number>string print ;
MAIN: main</
test.factor:
<
INCLUDING: scriptedmain ;
Line 492 ⟶ 592:
: main ( -- ) meaning-of-life "Test: The meaning of life is " write number>string print ;
MAIN: main</
=={{header|Forth}}==
Line 498 ⟶ 598:
Given this awful running reference:
<
: go ( -- )
." The meaning of life is " Douglas-Adams . cr ;</
The bulk of Forth systems provide a way to generate an executable that enters GO (ar any word) on start.
Line 507 ⟶ 607:
{{works with|SwiftForth|SwiftForth|4.0}}
<
program douglas-adams</
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 516 ⟶ 616:
{{works with|gforth}}
<
42 constant Douglas-Adams
.( The meaning of life is ) Douglas-Adams . cr bye</
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 525 ⟶ 625:
{{works with|gforth}}
<
42 constant Douglas-Adams
Line 533 ⟶ 633:
[THEN]
cr .( Why aren't you running this as a script? It only provides a constant.)</
=={{header|FreeBASIC}}==
{{trans|Ring}}
<syntaxhighlight lang="freebasic">
Function meaningoflife() As Byte
Dim As Byte y = 42
Return y
End Function
Sub main()
Print "Main: The meaning of life is "; meaningoflife()
End Sub
main()
Sleep
</syntaxhighlight>
{{out}}
<pre>
Main: The meaning of life is 42
</pre>
=={{header|Go}}==
Line 541 ⟶ 663:
First create these two files in the 'modulino' directory:
<
package main
Line 552 ⟶ 674:
func libMain() {
fmt.Println("The meaning of life is", MeaningOfLife())
}</
<
package main
func main() {
libMain()
}</
To emulate a modulino:
Line 570 ⟶ 692:
Now create this file in the 'mol' directory:
<
package main
Line 577 ⟶ 699:
func main() {
fmt.Println("The meaning of life is still", MeaningOfLife())
}</
and copy modulino.go to the 'mol' directory. The library can then be used in the 'normal' way:
{{out}}
Line 590 ⟶ 712:
Example:
<
Main: The meaning of life is 42
$ ./Test.groovy
Test: The meaning of life is 42</
ScriptedMain.groovy:
<
class ScriptedMain {
Line 605 ⟶ 727:
println "Main: The meaning of life is " + meaningOfLife
}
}</
Test.groovy:
<
println "Test: The meaning of life is " + ScriptedMain.meaningOfLife</
=={{header|Haskell}}==
Haskell has scripted main, but getting scripted main to work with compiled scripts is tricky.
<
Main: The meaning of life is 42
$ runhaskell test.hs
Line 625 ⟶ 747:
$ ghc -fforce-recomp -o test -main-is Test test.hs scriptedmain.hs
$ ./test
Test: The meaning of life is 42</
scriptedmain.hs
<
-- Compile:
Line 641 ⟶ 763:
main :: IO ()
main = putStrLn $ "Main: The meaning of life is " ++ show meaningOfLife</
test.hs
<
-- Compile:
Line 656 ⟶ 778:
main :: IO ()
main = putStrLn $ "Test: The meaning of life is " ++ show meaningOfLife</
=={{header|Io}}==
Line 662 ⟶ 784:
ScriptedMain.io:
<
ScriptedMain := Object clone
Line 669 ⟶ 791:
if( isLaunchScript,
"Main: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println
)</
test.io:
<
"Test: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println</
<
Main: The meaning of life is 42
$ ./test.io
Test: The meaning of life is 42</
=={{header|J}}==
Line 686 ⟶ 808:
modulinos.ijs:
<
meaningOfLife =: 42
Line 701 ⟶ 823:
)
shouldrun 0</
test.j:
<
load 'modulinos.ijs'
Line 711 ⟶ 833:
echo 'Test: The meaning of life is ',": meaningOfLife
exit ''</
Example:
<
Main: The meaning of life is 42
$ ./test.j
Test: The meaning of life is 42</
=={{header|Java}}==
Line 725 ⟶ 847:
ScriptedMain.java
<
public static int meaningOfLife() {
return 42;
Line 733 ⟶ 855:
System.out.println("Main: The meaning of life is " + meaningOfLife());
}
}</
Test.java
<
public static void main(String[] args) {
System.out.println("Test: The meaning of life is " + ScriptedMain.meaningOfLife());
}
}</
=={{header|JavaScript}}==
Line 748 ⟶ 870:
scriptedmain.js
<
function meaningOfLife() { return 42; }
Line 758 ⟶ 880:
}
if (!module.parent) { main(); }</
test.js
<
var sm = require("./scriptedmain");
console.log("Test: The meaning of life is " + sm.meaningOfLife());</
=={{header|Julia}}==
Line 771 ⟶ 893:
<br />
In module file Divisors.jl:
<
using Primes
Line 812 ⟶ 934:
Divisors.interactiveDivisors()
end
</syntaxhighlight>
In a user file getdivisors.jl:
<
using .Divisors
Line 820 ⟶ 942:
n = 708245926330
println("The proper divisors of $n are ", properdivisors(n))
</syntaxhighlight>
=={{header|LLVM}}==
LLVM can have scripted main a la C, using the weak attribute.
<
llvm-as scriptedmain.ll
llc scriptedmain.bc
Line 835 ⟶ 957:
gcc -o test test.s scriptedmain.s
./test
Test: The meaning of life is 42</
Makefile
<
EXECUTABLE_TEST=test
Line 860 ⟶ 982:
-rm test.bc
-rm scriptedmain.s
-rm scriptedmain.bc</
scriptedmain.ll
<
declare i32 @printf(i8* noalias nocapture, ...)
Line 878 ⟶ 1,000:
ret i32 0
}</
test.ll
<
declare i32 @printf(i8* noalias nocapture, ...)
Line 894 ⟶ 1,016:
ret i32 0
}</
=={{header|Lua}}==
Line 901 ⟶ 1,023:
scriptedmain.lua
<
function meaningoflife()
Line 915 ⟶ 1,037:
else
module(..., package.seeall)
end</
test.lua
<
sm = require("scriptedmain")
print("Test: The meaning of life is " .. sm.meaningoflife())</
=={{header|Make}}==
Example
<
The meaning of life is 42
(Main)
$ make -f test.mf
The meaning of life is 42
(Test)</
scriptedmain.mf
<
meaning-of-life:
Line 941 ⟶ 1,063:
scriptedmain: meaning-of-life
@echo "(Main)"
</syntaxhighlight>
test.mf
<
test:
@make -f scriptedmain.mf meaning-of-life
@echo "(Test)"
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
scriptedmain.ma
<
MeaningOfLife[] = 42
Line 969 ⟶ 1,091:
If[StringMatchQ[Program, ".*scriptedmain.*"],
Print["Main: The meaning of life is " <> ToString[MeaningOfLife[]]]
]</
test.ma:
<
Get["scriptedmain.ma"]
Print["Test: The meaning of life is " <> ToString[MeaningOfLife[]]]</
Example:
<
Main: The meaning of life is 42
$ ./test.ma
Test: The meaning of life is 42</
In Mac and Windows, the output will be surrounded by spurious quotes.
Line 988 ⟶ 1,110:
=={{header|Mozart/Oz}}==
Makefile:
<
run: scriptedmain test
Line 1,008 ⟶ 1,130:
-rm *.ozf
-rm *.exe
</syntaxhighlight>
scriptedmain.oz:
<
export
meaningOfLife: MeaningOfLife
Line 1,029 ⟶ 1,151:
end
end
</syntaxhighlight>
test.oz:
<
import
ScriptedMain
Line 1,046 ⟶ 1,168:
end
end
end</
=={{header|newLISP}}==
Line 1,053 ⟶ 1,175:
scriptedmain.lsp
<
(context 'SM)
Line 1,065 ⟶ 1,187:
(if (find "scriptedmain" (main-args 1)) (main))
(context MAIN)</
test.lsp
<
(load "scriptedmain.lsp")
(println (format "Test: The meaning of life is %d" (SM:meaning-of-life)))
(exit)</
=={{header|Nim}}==
Nim provides the predicate <code>isMainModule</code> to use with conditional compilation. Here is an example:
<syntaxhighlight lang="Nim">proc p*() =
## Some exported procedure.
echo "Executing procedure"
# Some code to execute to initialize the module.
echo "Initializing the module"
when isMainModule:
# Some code to execute if the module is run directly, for instance code to test the module.
echo "Running tests"
</syntaxhighlight>
{{out}}
When run directly, the result of execution is:
<pre>Initializing the module
Running tests
</pre>
If we call “p” from another module, we get:
<pre>Initializing the module
Executing procedure
</pre>
=={{header|Objective-C}}==
Line 1,079 ⟶ 1,226:
scriptedmain.h:
<
@interface ScriptedMain: Object {}
Line 1,085 ⟶ 1,232:
+ (int)meaningOfLife;
@end</
scriptedmain.m:
<
#import <Foundation/Foundation.h>
Line 1,108 ⟶ 1,255:
return 0;
}</
test.m:
<
#import <Foundation/Foundation.h>
Line 1,123 ⟶ 1,270:
return 0;
}</
<
$ gcc -o test -lobjc -framework foundation test.m scriptedmain.m
$ ./scriptedmain
Main: The meaning of life is 42
$ ./test
Test: The meaning of life is 42</
=={{header|OCaml}}==
Line 1,136 ⟶ 1,283:
scriptedmain.ml
<
let main () =
Line 1,144 ⟶ 1,291:
let () =
if not !Sys.interactive then
main ()</
Invoked as a script:
<
Main: The meaning of life is 42</
Loaded into an ocaml toplevel/utop:
<syntaxhighlight lang="text">$ ocaml
...
# #use "scriptedmain.ml";;
Line 1,160 ⟶ 1,307:
# meaning_of_life;;
- : int = 42
# </
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,169 ⟶ 1,316:
meaningoflife.m
<
function y = meaningoflife()
Line 1,179 ⟶ 1,326:
endfunction
main();</
test.m
<
printf("Test: The meaning of life is %d", meaningoflife());</
=={{header|Pascal}}==
Line 1,191 ⟶ 1,338:
Makefile:
<
scriptedmain: scriptedmain.pas
Line 1,203 ⟶ 1,350:
-rm scriptedmain
-rm *.o
-rm *.ppu</
scriptedmain.pas:
<
program ScriptedMain;
{$ELSE}
Line 1,224 ⟶ 1,371:
writeln(MeaningOfLife())
{$ENDIF}
end.</
test.pas:
<
uses
ScriptedMain;
Line 1,234 ⟶ 1,381:
write('Test: The meaning of life is: ');
writeln(MeaningOfLife())
end.</
Example:
<
$ ./scriptedmain
Main: The meaning of life is: 42
$ make test
$ ./test
Test: The meaning of life is: 42</
=={{header|Perl}}==
Perl has scripted main. The code inside <tt>unless(caller) { ... }</tt> only runs when <tt>Life.pm</tt> is the main program.
<
# Life.pm
Line 1,262 ⟶ 1,409:
unless(caller) {
print "Main: The meaning of life is " . meaning_of_life() . "\n";
}</
<
# death.pl
Line 1,273 ⟶ 1,420:
print "Life means " . Life::meaning_of_life . ".\n";
print "Death means invisible scary skeletons.\n";</
=={{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.
<!--<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: #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}}==
Line 1,284 ⟶ 1,434:
scriptedmain.php
<
function meaning_of_life() {
return 42;
Line 1,296 ⟶ 1,446:
main($argv);
}
?></
test.php
<
require_once("scriptedmain.php");
echo "Test: The meaning of life is " . meaning_of_life() . "\n";
?></
=={{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":
<
(de meaningOfLife ()
Line 1,314 ⟶ 1,464:
(de lifemain ()
(prinl "Main: The meaning of life is " (meaningOfLife))
(bye) )</
and an executable file (chmod +x) "test.l":
<
(load "life.l")
(prinl "Test: The meaning of life is " (meaningOfLife))
(bye)</
Test:
<pre>$ ./life.l -lifemain
Line 1,332 ⟶ 1,482:
Python has scripted main.
<
# life.py
Line 1,340 ⟶ 1,490:
if __name__ == "__main__":
print("Main: The meaning of life is %s" % meaning_of_life())</
<
# death.py
Line 1,349 ⟶ 1,499:
print("Life means %s." % meaning_of_life())
print("Death means invisible scary skeletons.")</
=={{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.)
<
meaningOfLife <- function() {
Line 1,368 ⟶ 1,518:
main(args)
q("no")
}</
test.R
<
source("scriptedmain.R")
Line 1,378 ⟶ 1,528:
cat("Test: The meaning of life is", meaningOfLife(), "\n")
q("no")</
=={{header|Racket}}==
scriptedmain.rkt:
<
#lang racket
Line 1,389 ⟶ 1,539:
(define (meaning-of-life) 42)
(module+ main (printf "Main: The meaning of life is ~a\n" (meaning-of-life)))</
test.rkt:
<
#lang racket
(module+ main
(require "scriptedmain.rkt")
(printf "Test: The meaning of life is ~a\n" (meaning-of-life)))</
=={{header|Raku}}==
(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".
<syntaxhighlight lang="raku"
has $.answer = 42;
}
Line 1,412 ⟶ 1,562:
multi MAIN ('methods') {
say ~LUE.^methods;
}</
=={{header|REXX}}==
<
parse source . howInvoked @fn /*query REXX how this pgm got invoked. */
Line 1,432 ⟶ 1,582:
/*────────────────────────────── The main code follows here ... ────────────────────────*/
say
say '(from' @fn"): and away we go ···"</
=={{header|Ring}}==
<
# Project : Modulinos
Line 1,444 ⟶ 1,594:
func main()
see "Main: The meaning of life is " + meaningoflife() + nl
</syntaxhighlight>
Output:
<pre>
Line 1,453 ⟶ 1,603:
Ruby has scripted main.
<
def meaning_of_life
Line 1,461 ⟶ 1,611:
if __FILE__ == $0
puts "Main: The meaning of life is #{meaning_of_life}"
end</
<
require 'life'
puts "Life means #{meaning_of_life}."
puts "Death means invisible scary skeletons."</
=={{header|Rust}}==
Line 1,474 ⟶ 1,624:
Makefile:
<
scriptedmain: scriptedmain.rs
Line 1,487 ⟶ 1,637:
-rm -rf *.dylib
-rm scriptedmain
-rm -rf *.dSYM</
scriptedmain.rs:
<
use std;
Line 1,500 ⟶ 1,650:
fn main() {
std::io::println("Main: The meaning of life is " + core::int::to_str(meaning_of_life(), 10u));
}</
test.rs:
<
use std;
fn main() {
std::io::println("Test: The meaning of life is " + core::int::to_str(scriptedmain::meaning_of_life(), 10u));
}</
Example:
<
$ make test
$ ./scriptedmain
Main: The meaning of life is 42
$ ./test
Test: The meaning of life is 42</
=={{header|SAC}}==
Makefile:
<
scriptedmain: ScriptedMain.sac
Line 1,536 ⟶ 1,686:
-rm libScriptedMainMod.a
-rm scriptedmain
-rm scriptedmain.c</
ScriptedMain.sac:
<
module ScriptedMain;
#endif
Line 1,556 ⟶ 1,706:
return(0);
}
#endif</
test.sac:
<
use Array: all;
use ScriptedMain: all;
Line 1,566 ⟶ 1,716:
printf("Test: The meaning of life is %d\n", meaning_of_life());
return(0);
}</
Example:
<
$ make test
$ ./scriptedmain
Main: The meaning of life is 42
$ ./test
Test: The meaning of life is 42</
=={{header|Scala}}==
Line 1,582 ⟶ 1,732:
===Unix shell script===
This code must be stored as a shell script.
<
exec scala "$0" "$@"
!#
Line 1,592 ⟶ 1,742:
println(s"Use the routine to show that the hailstone sequence for the number: $nr.")
println(collatz.toList)
println(s"It has ${collatz.length} elements.")</
===Windows Command Script===
This code must be stored as a Windows Command Script e.g. Hailstone.cmd
<
@echo off
call scala %0 %*
Line 1,610 ⟶ 1,760:
println(collatz.toList)
println(s"It has ${collatz.length} elements.")
</syntaxhighlight>
{{out}}
<pre>C:\>Hailstone.cmd 42
Line 1,623 ⟶ 1,773:
scriptedmain.scm
<
#|
exec csi -ss $0 ${1+"$@"}
Line 1,647 ⟶ 1,797:
(if (equal? (car (program)) 'compiled)
(main (cdr (argv))))</
test.scm
<
#|
exec csi -ss $0 ${1+"$@"}
Line 1,659 ⟶ 1,809:
(load "scriptedmain.scm")
(display (format "Test: The meaning of life is ~a\n" (meaning-of-life)))
(exit))</
=={{header|Sidef}}==
<
func meaning_of_life {
Line 1,670 ⟶ 1,820:
if (__FILE__ == __MAIN__) {
say "Main: The meaning of life is #{meaning_of_life()}"
}</
<
include Life
say "Test: The meaning of life is #{Life::meaning_of_life()}."</
=={{header|Smalltalk}}==
Line 1,684 ⟶ 1,834:
Example
<
$ ./scriptedmain.st
Line 1,690 ⟶ 1,840:
$ ./test.st
Test: The meaning of life is 42</
package.xml
<
<package>
<name>ScriptedMain</name>
Line 1,700 ⟶ 1,850:
<file>scriptedmain.st</file>
</package>
</packages></
scriptedmain.st
<
"exit"
Line 1,719 ⟶ 1,869:
(((Smalltalk getArgc) > 0) and: [ ((Smalltalk getArgv: 1) endsWith: 'scriptedmain.st') ]) ifTrue: [
main value.
].</
test.st
<
"exit"
Line 1,729 ⟶ 1,879:
PackageLoader fileInPackage: 'ScriptedMain'.
Transcript show: 'Test: The meaning of life is ', ((ScriptedMain meaningOfLife) printString); cr.</
=={{header|Swift}}==
Line 1,737 ⟶ 1,887:
Example
<
mkdir -p bin/
swiftc -D SCRIPTEDMAIN -o bin/ScriptedMain ScriptedMain.swift
Line 1,746 ⟶ 1,896:
Main: The meaning of life is 42
bin/Test
Test: The meaning of life is 42</
Makefile
<
bin/ScriptedMain
bin/Test
Line 1,770 ⟶ 1,920:
-rm *.swiftdoc
-rm *.dylib
</syntaxhighlight>
ScriptedMain.swift
<
public class ScriptedMain {
Line 1,795 ⟶ 1,945:
}
#endif
</syntaxhighlight>
Test.swift
<
import ScriptedMain
Line 1,817 ⟶ 1,967:
}
#endif
</syntaxhighlight>
=={{header|Tcl}}==
<
puts "Directory: [pwd]"
puts "Program: $::argv0"
Line 1,829 ⟶ 1,979:
if {$::argv0 eq [info script]} {
main {*}$::argv
}</
=={{header|UNIX Shell}}==
Line 1,836 ⟶ 1,986:
scriptedmain.sh
<
meaning_of_life() {
Line 1,850 ⟶ 2,000:
then
main
fi</
test.sh
<
path=$(dirname -- "$0")
Line 1,861 ⟶ 2,011:
meaning_of_life
echo "Test: The meaning of life is $?"
</syntaxhighlight>
=={{header|Wren}}==
As far as Wren is concerned, a modulino and an executable library seem to be different names for the same thing. This therefore uses the same technique as the [[Executable_library#Wren]] task to create a simple modulino.
Note that Wren doesn't need or normally use a ''main()'' function to start a script, though we use one here to make the example clearer.
First we create a module for our modulino:
<syntaxhighlight lang="wren">/* Modulinos.wren */
var MeaningOfLife = Fn.new { 42 }
var main = Fn.new {
System.print("The meaning of life is %(MeaningOfLife.call()).")
}
// Check if it's being used as a library or not.
import "os" for Process
if (Process.allArguments[1] == "Modulinos.wren") { // if true, not a library
main.call()
}</syntaxhighlight>
and run it to make sure it works OK when run directly:
{{output}}
<pre>
The meaning of life is 42.
</pre>
Next we create another module which imports the modulino:
<syntaxhighlight lang="wren">/* Modulinos_main.wren */
import "./Modulinos" for MeaningOfLife
var main = Fn.new {
System.print("Who says the meaning of life is %(MeaningOfLife.call())?")
}
main.call()</syntaxhighlight>
and run this to make sure the modulino's ''main()'' function doesn't run:
{{output}}
<pre>
Who says the meaning of life is 42?
</pre>
=={{header|ZX Spectrum Basic}}==
Line 1,867 ⟶ 2,060:
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:
<
{{omit from|Ada}}
|