Modulinos

From Rosetta Code
Revision as of 23:49, 5 August 2011 by 129.174.188.117 (talk)
Modulinos is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

It is useful to be able to execute a main() function only when a program is run directly. This is a central feature in programming scripts; the feature is called scripted main.

Examples from GitHub.

Related to ScriptName

This is still a draft task, and the current task description has caused mega confusion. See Talk:Scripted Main for numerous attempts to understand the task and to rewrite the task description.
The task Executable library is written to replace this task. This task's future is in doubt as its aims are not clear enough.

C

C programs cannot normally do scripted main, because main() is implicitly included by another program, test.c, even though scriptedmain.h omits any main() prototype. A compiler directive fixes this.

<lang sh>$ gcc -o scriptedmain scriptedmain.c scriptedmain.h $ ./scriptedmain Main: The meaning of life is 42 $ gcc -o test test.c scriptedmain.c scriptedmain.h $ ./test Test: The meaning of life is 42</lang>

scriptedmain.h

<lang c>int meaning_of_life();</lang>

scriptedmain.c

<lang c>#include <stdio.h>

int meaning_of_life() { return 42; }

int __attribute__((weak)) main(int argc, char **argv) { printf("Main: The meaning of life is %d\n", meaning_of_life());

return 0; }</lang>

test.c

<lang c>#include "scriptedmain.h"

  1. include <stdio.h>

extern int meaning_of_life();

int main(int argc, char **argv) { printf("Test: The meaning of life is %d\n", meaning_of_life()); return 0; }</lang>

C++

C++ programs cannot normally do scripted main, because main() is implicitly included by another program, test.c, even though scriptedmain.h omits any main() prototype. A compiler directive fixes this.

scriptedmain.h

<lang cpp>int meaning_of_life();</lang>

scriptedmain.cpp

<lang cpp>#include <iostream>

using namespace std;

int meaning_of_life() { return 42; }

int __attribute__((weak)) main(int argc, char **argv) { cout << "Main: The meaning of life is " << meaning_of_life() << endl;

return 0; }</lang>

test.cpp

<lang cpp>#include "scriptedmain.h"

  1. include <iostream>

using namespace std;

extern int meaning_of_life();

int main(int argc, char **argv) { cout << "Test: The meaning of life is " << meaning_of_life() << endl;

return 0; }</lang>

Chicken Scheme

Chicken Scheme has the {{{ -ss }}} flag for the interpreter, but compiled Chicken Scheme programs do not have scripted main unless the behavior is added manually to the code.

scriptedmain.scm

<lang scheme>#!/bin/bash

  1. |

exec csi -ss $0 ${1+"$@"} exit |#

(use posix) (require-extension srfi-1) ; lists

(define (meaning-of-life) 42)

(define (main args) (display (format "Main: The meaning of life is ~a\n" (meaning-of-life))) (exit))

(define (program) (if (string=? (car (argv)) "csi") (let ((s-index (list-index (lambda (x) (string-contains x "-s")) (argv)))) (if (number? s-index) (cons 'interpreted (list-ref (argv) (+ 1 s-index))) (cons 'unknown ""))) (cons 'compiled (car (argv)))))

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

test.scm

<lang scheme>#!/bin/bash

  1. |

exec csi -ss $0 ${1+"$@"} exit |# (define (main args) (load "scriptedmain.scm") (display (format "Test: The meaning of life is ~a\n" (meaning-of-life))) (exit))</lang>

Clojure

Clojure has a function -main which will run in only four cases.

  1. clj is passed the flag -m <class>. <lang sh>$ clj -m scriptedmain

Main: The meaning of life is 42 $ clj -m test Test: The meaning of life is 42</lang>

  1. A shebang forces -m <class>. <lang sh>$ ./scriptedmain.clj

Main: The meaning of life is 42 $ ./test.clj Test: The meaning of life is 42</lang>

  1. The compiled class is run. <lang sh>$ java -cp ~/Library/Clojure/lib/clojure.jar:. scriptedmain

Main: The meaning of life is 42 $ java -cp ~/Library/Clojure/lib/clojure.jar:. test Test: The meaning of life is 42</lang>

  1. -main is explicitly called. <lang sh>

$ clj user=> (load-file "scriptedmain.clj") user=> (scriptedmain/-main) Main: The meaning of life is 42 nil</lang>

scriptedmain.clj

<lang lisp>":";exec clj -m `basename $0 .clj` ${1+"$@"} ":";exit

(ns scriptedmain (:gen-class))

(defn meaning-of-life [] 42)

(defn -main [& args] (println "Main: The meaning of life is" (meaning-of-life)))</lang>

test.clj

<lang lisp>":";exec clj -m `basename $0 .clj` ${1+"$@"} ":";exit

(ns test (:gen-class))

(load "scriptedmain")

(defn -main [& args] (println "Test: The meaning of life is" (scriptedmain/meaning-of-life)))</lang>

Common Lisp

Common Lisp has few standards for POSIX operation. Shebangs and command line arguments are hacks.

In CLISP, this code only works for ./scriptedmain.lisp.

~/.clisprc.lisp

<lang lisp>;;; Play nice with shebangs (set-dispatch-macro-character #\# #\!

(lambda (stream character n)
 (declare (ignore character n))
 (read-line stream nil nil t)
 nil))</lang>

scriptedmain.lisp

<lang lisp>#!/bin/bash

  1. |

exec clisp -q -q $0 $0 ${1+"$@"} exit |#

Usage
./scriptedmain.lisp

(defun meaning-of-life () 42)

(defun main (args)

(format t "Main: The meaning of life is ~a~%" (meaning-of-life))
(quit))
With help from Francois-Rene Rideau
http://tinyurl.com/cli-args

(let ((args

      #+clisp ext:*args*
      #+sbcl sb-ext:*posix-argv*
      #+clozure (ccl::command-line-arguments)
      #+gcl si:*command-args*
      #+ecl (loop for i from 0 below (si:argc) collect (si:argv i))
      #+cmu extensions:*command-line-strings*
      #+allegro (sys:command-line-arguments)
      #+lispworks sys:*line-arguments-list*
    ))
 (if (member (pathname-name *load-truename*)
             args
             :test #'(lambda (x y) (search x y :test #'equalp)))
   (main args)))</lang>

test.lisp

<lang lisp>#!/bin/bash

  1. |

exec clisp -q -q $0 $0 ${1+"$@"} exit |#

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

Emacs Lisp

Emacs has scripted main, though older versions require an obscure shebang syntax.

scriptedmain.el

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

Shebang from John Swaby
http://www.emacswiki.org/emacs/EmacsScripts

(defun meaning-of-life () 42)

(defun main ()

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

test.el

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

Shebang from John Swaby
http://www.emacswiki.org/emacs/EmacsScripts

(defun main ()

(setq load-path (cons default-directory load-path))
(load "scriptedmain.el" nil t)
(message "Test: The meaning of life is %d" (meaning-of-life)))</lang>

Erlang

Erlang has scripted main by default. scriptedmain.erl must be compiled before test.erl can access its functions.

scriptedmain.erl

<lang erlang>-module(scriptedmain). -export([meaning_of_life/0]). -import(lists, [map/2]).

meaning_of_life() -> 42.

main(_) -> io:format("Main: The meaning of life is ~w~n", [meaning_of_life()]).</lang>

test.erl

<lang erlang>% Compile scriptedmain.erl first.

-module(test). -import(scriptedmain, [meaning_of_life/0]).

main(_) -> io:format("Test: The meaning of life is ~w~n", [meaning_of_life()]).</lang>

Factor

This "fibonacci" vocabulary exports both a fib word (from Fibonacci sequence#Factor) and a main entry point.

<lang factor>USING: kernel io math math.parser sequences ; IN: fibonacci

fib ( n -- m )
   dup 2 < [
       [ 0 1 ] dip [ swap [ + ] keep ] times
       drop
   ] unless ;
first-15 ( -- )
   15 iota [
       dup
       fib number>string write
       14 = [ "\n" ] [ ", " ] if write
   ] each ;

MAIN: first-15</lang>

One can run the main program from the listener:

( scratchpad ) "fibonacci" run
Loading resource:work/fibonacci/fibonacci.factor
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377

One can also run the main program as a script:

$ ./factor run=fibonacci
Loading resource:work/fibonacci/fibonacci.factor
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377

One can also use the exported words (in the listener, or in another program):

( scratchpad ) USE: fibonacci
( scratchpad ) 20 fib .
6765

Haskell

Haskell has scripted main, but getting scripted main to work with compiled scripts is tricky.

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

scriptedmain.hs

<lang haskell>#!/usr/bin/env runhaskell

-- Compile: -- -- ghc -fforce-recomp -o scriptedmain -main-is ScriptedMain scriptedmain.hs

module ScriptedMain where

meaningOfLife :: Int meaningOfLife = 42

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

test.hs

<lang haskell>#!/usr/bin/env runhaskell

-- Compile: -- -- ghc -fforce-recomp -o test -main-is Test test.hs scriptedmain.hs

module Test where

import ScriptedMain hiding (main)

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

J

Probably the simplest way to achive what I imagine "scripted main" to be, in J, involves the use of the Immex Phrase. Here, just before the script ends, you define the "main" which would take control if the script was used as a stand alone program.

Here is an example "scripted main" program, using this approach:

<lang j>NB. example "scripted main" code, saved as sm.ijs myName=: 'My name is George' 9!:29]1 9!:27'smoutput myName'</lang>

Here is an example consumer, which is another "scripted main" program:

<lang j>NB. example "alternate main" code require'sm.ijs' 9!:29]1 9!:27'smoutput length: ,":#myName'</lang>

Here is another example consumer. This example is library code, without any main:

<lang j>NB. example "non main" library code require'sm.ijs' 9!:29]0 reversed=:|.myName</lang>

Example use:

<lang j> load 'sm.ijs' My name is George

  load 'smalt.ijs'

length: 17

  load 'smlib.ijs'
  reversed

egroeG si eman yM</lang>

This could also be used from the command line, for example jconsole sm.ijs and so on...

Java

Java has scripted main by default.

ScriptedMain.java

<lang java>public class ScriptedMain { public static int meaningOfLife() { return 42; }

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

Test.java

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

LLVM

LLVM can have scripted main a la C, using the weak attribute.

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

Makefile

<lang make>EXECUTABLE_SM=scriptedmain EXECUTABLE_TEST=test

all: test.ll scriptedmain.s llvm-as test.ll llc test.bc gcc -o $(EXECUTABLE_TEST) test.s scriptedmain.s ./$(EXECUTABLE_TEST)

scriptedmain.s: scriptedmain.ll llvm-as scriptedmain.ll llc scriptedmain.bc gcc -o $(EXECUTABLE_SM) scriptedmain.s ./$(EXECUTABLE_SM)

clean: -rm $(EXECUTABLE_TEST) -rm $(EXECUTABLE_SM) -rm test.s -rm test.bc -rm scriptedmain.s -rm scriptedmain.bc</lang>

scriptedmain.ll

<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, ...)

define i32 @meaning_of_life() { ret i32 42 }

define weak i32 @main(i32 %argc, i8** %argv) { %meaning = call i32 @meaning_of_life()

call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([33 x i8]* @msg_main, i32 0, i32 0), i32 %meaning)

ret i32 0 }</lang>

test.ll

<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 @meaning_of_life()

define i32 @main(i32 %argc, i8** %argv) { %meaning = call i32 @meaning_of_life()

call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([33 x i8]* @msg_test, i32 0, i32 0), i32 %meaning)

ret i32 0 }</lang>

Lua

Lua has scripted main by default because files are largely indistinguishable from functions semantically (they compile to Lua functions.) Ellipses is Lua's var-arg syntax for functions, and, therefore, for files as well.

scriptedmain.lua

<lang lua>#!/usr/bin/env lua

function meaningoflife() return 42 end

function main(arg) print("Main: The meaning of life is " .. meaningoflife()) end

if type(package.loaded[(...)]) ~= "userdata" then main(arg) else module(..., package.seeall) end</lang>

test.lua

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

newLISP

newLISP lacks scripted main, but the feature is easily added.

scriptedmain.lsp

<lang lisp>#!/usr/bin/env newlisp

(context 'SM)

(define (SM:meaning-of-life) 42)

(define (main) (println (format "Main: The meaning of life is %d" (meaning-of-life))) (exit))

(if (find "scriptedmain" (main-args 1)) (main))

(context MAIN)</lang>

test.lsp

<lang lisp>#!/usr/bin/env newlisp

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

Node.js

Node.js has scripted main.

scriptedmain.js

<lang javascript>#!/usr/bin/env node

function meaningOfLife() { return 42; }

exports.meaningOfLife = meaningOfLife;

function main() { console.log("Main: The meaning of life is " + meaningOfLife()); }

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

test.js

<lang javascript>#!/usr/bin/env node

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

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

Octave/MATLAB

Octave and MATLAB have scripted main by default, because only the first function listed in a program are importable by other programs.

meaningoflife.m

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

function y = meaningoflife() y = 42; endfunction

function main() printf("Main: The meaning of life is %d", meaningoflife()); endfunction

main();</lang>

test.m

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

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

Perl

Perl has scripted main. The code inside unless(caller) { ... } only runs when Life.pm is the main program.

<lang perl>#!/usr/bin/env perl

  1. Life.pm

package Life;

use strict; use warnings;

sub meaning_of_life { return 42; }

unless(caller) { print "Main: The meaning of life is " . meaning_of_life() . "\n"; }</lang>

<lang perl>#!/usr/bin/env perl

  1. death.pl

use strict; use warnings;

use Life;

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

Perl 6

Perl 6 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 {

   has $.answer = 42;

}

multi MAIN ('test') {

   say "ok" if LUE.new.answer == 42;

}

multi MAIN ('methods') {

   say ~LUE.^methods;

}</lang>

PHP

PHP does not have scripted main, but the feature is easily added with a regular expression.

scriptedmain.php

<lang php><?php function meaning_of_life() { return 42; }

function main($args) { echo "Main: The meaning of life is " . meaning_of_life() . "\n"; }

if (preg_match("/scriptedmain/", $_SERVER["SCRIPT_NAME"])) { main($argv); } ?></lang>

test.php

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

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": <lang PicoLisp>#!/usr/bin/picolisp /usr/lib/picolisp/lib.l

(de meaningOfLife ()

  42 )

(de lifemain ()

  (prinl "Main: The meaning of life is " (meaningOfLife))
  (bye) )</lang>

and an executable file (chmod +x) "test.l": <lang PicoLisp>#!/usr/bin/picolisp /usr/lib/picolisp/lib.l

(load "life.l")

(prinl "Test: The meaning of life is " (meaningOfLife)) (bye)</lang> Test:

$ ./life.l -lifemain
Main: The meaning of life is 42

$ ./test.l
Test: The meaning of life is 42

Python

Python has scripted main.

<lang python>#!/usr/bin/env python

  1. life.py

def meaning_of_life():

 return 42

if __name__ == "__main__":

 print "Main: The meaning of life is %s" % meaning_of_life()</lang>

<lang python>#!/usr/bin/env python

  1. death.py

from life import meaning_of_life

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

R

R does not have scripted main, but the feature is easily added with regular expressions.

scriptedmain.R

<lang R>#!/usr/bin/Rscript

meaningOfLife <- function() { 42 }

main <- function(program, args) { cat("Main: The meaning of life is", meaningOfLife(), "\n") }

getProgram <- function(args) { sub("--file=", "", args[grep("--file=", args)]) }

args <- commandArgs(trailingOnly = FALSE) program <- getProgram(args)

if (length(program) > 0 && length(grep("scriptedmain", program)) > 0) { main(program, args) q("no") }</lang>

test.R

<lang R>#!/usr/bin/Rscript

source("scriptedmain.R")

cat("Test: The meaning of life is", meaningOfLife(), "\n")

q("no")</lang>

Ruby

Ruby has scripted main.

<lang ruby># life.rb

def meaning_of_life

 42

end

if __FILE__ == $0

 puts "Main: The meaning of life is #{meaning_of_life}"

end</lang>

<lang ruby># death.rb

require 'life'

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

Tcl

<lang tcl>proc main {args} {

   puts "Directory: [pwd]"
   puts "Program: $::argv0"
   puts "Number of args: [llength $args]"
   foreach arg $args {puts "Arg: $arg"}

}

if {$::argv0 eq [info script]} {

   main {*}$::argv

}</lang>

UNIX Shell

Bash has scripted main.

scriptedmain.sh

<lang sh>#!/usr/bin/env sh

meaning_of_life() { return 42 }

main() { meaning_of_life echo "Main: The meaning of life is $?" }

if "$BASH_SOURCE" == "$0" then

   main

fi</lang>

test.sh

<lang sh>#!/usr/bin/env sh source scriptedmain.sh meaning_of_life echo "Test: The meaning of life is $?"</lang>

ZX Spectrum Basic

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>