Native shebang: Difference between revisions

m
Clarify: this task must be done in the specimen language, '''neither''' using a shell script '''nor''' any other 3rd language.
m (Clarify: this task must be done in the specimen language, '''neither''' using a shell script '''nor''' any other 3rd language.)
Line 1:
{{clarify task}}{{draft task|Basic language learning}}
'''In short:''' Use the specimen language for "scripting".
 
Example: If your language is "foo", then the test case of "echo.foo" run in a terminal as "./echo.foo Hello, world!".
 
'''In long''': Create a program (in the specimen language) that will automatically compile a ''test case'' (of the same specimen language) to a native binary executable and then transparently load and run this ''test case'' executable.
Make it so that all that is required is a custom [[wp:Shebang (Unix)|shebangs]] at the start of the ''test case''.
 
'''Importantly:''' This task must be coded '''strictly''' in the specimen language, '''neither''' using a shell script '''nor''' any other 3rd language.
 
Optimise this progress so that the ''test program'' '''binary executable''' is only created if the original ''test program'' '''source code''' as been touched/edited.
 
Note: If the lauguage (or a specific implementation) handles this automatically, then simple provide an example of "echo.foo"
 
'''Background:'''
 
Simple [[wp:Shebang (Unix)|shebangs]] can help with scripting, e.g. "<tt>#!/usr/bin/env python</tt>" at the top of a [[Python]] script will allow it to be run in a terminal as "./script.py".
 
The task [[Multiline shebang]] largely demonstrates how to use "shell" code in the shebang to compile and/or run source-code from a 3rd language.
 
'''This task:'''
 
However in this task '''Native shebang''' task we are go ''native''. In the shebang, instead of running a shell, we call a binary-executable generated from the original native language, e.g. when using [[C]] with gcc "<tt>#!/usr/local/bin/script_gcc</tt>" to extract, compile and run the native "script" source code.
Line 280 ⟶ 298:
</pre>
'''Test Output:'''
<pre>
Hello, world!
</pre>
 
=={{header|python}}==
Extract: "If you need to create a .pyc file for a module that is not imported, you can use the py_compile and compileall modules. The py_compile module can manually compile any module. One way is to use the py_compile.compile function in that module interactively:[http://effbot.org/pyfaq/how-do-i-create-a-pyc-file.htm]:"
<pre>
>>> import py_compile
>>> py_compile.compile('echo.py')
</pre>
 
'''File: echo.py'''
<lang python>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
print " ".join(sys.argv[1:])</lang>
 
'''Usage:'''
<pre>
./echo.py Hello, world!
</pre>
'''Output:'''
<pre>
Hello, world!