Native shebang: Difference between revisions

Content added Content deleted
m (env is a program, not a directory.)
Line 725: Line 725:
<pre>
<pre>
Hello, world!
Hello, world!
</pre>

=={{header|zkl}}==
This isn't something that is usually done with zkl as the compiler is fast enough (for scripts) to just run the source and let it get compiled and run. But, it can be done.

Since the #! parsing is done by a compiler front end and was designed to be used from the command line, we'll do that by forking zkl to compile the source if it is newer than the binary.
<lang zkl>#!/home/craigd/Bin/zkl

// This file: nativeShebang.zkl
// zkl --#! . -c nativeShebang -o.
// chmod a+x nativeShebang.z*
// ./nativeShebang.zsc


// If this [source] file is newer than the compiled version (or the
// compiled file doesn't exist), compile up a new version

srcName :=__FILE__;
compiledName:=File.splitFileName(srcName)[0,-1].concat() + ".zsc";
if(not File.exists(compiledName) or
File.info(srcName)[2] > File.info(compiledName)[2]){ // compare modifed dates
System.cmd("zkl --#! . -c %s -o. --exit".fmt(srcName));
System.cmd("chmod a+x %s".fmt(compiledName));
}
println("Hello world!");</lang>
{{out}}
<pre>
#run the source in the usual manner to generate a binary
$ zkl nativeShebang.zkl
Compiled Class(nativeShebang) (0.0 seconds, ??? lines/sec)
Wrote Class(nativeShebang) to ./nativeShebang.zsc
Hello world!

#eyeball the binary
$ zkl hexDump nativeShebang.zsc
0: 23 21 2f 68 6f 6d 65 2f | 63 72 61 69 67 64 2f 42 #!/home/craigd/B
16: 69 6e 2f 7a 6b 6c 0a 7a | 6b 44 00 00 28 31 2e 31 in/zkl.zkD..(1.1
32: 00 5a 4b 4c 20 53 65 72 | 69 61 6c 69 7a 65 64 20 .ZKL Serialized
48: 43 6c 61 73 73 00 6e 61 | 74 69 76 65 53 68 65 62 Class.nativeSheb
64: 61 6e 67 00 00 02 00 00 | 34 2b 61 74 74 72 69 62 ang.....4+attrib
80: 75 74 65 73 3a 73 74 61 | 74 69 63 20 63 72 65 61 utes:static crea
96: 74 65 52 65 74 75 72 6e | 73 53 65 6c 66 00 6e 61 teReturnsSelf.na
112: 74 69 76 65 53 68 65 62 | 61 6e 67 00 00 00 01 00 tiveShebang.....
...

#run the binary
$ ./nativeShebang.zsc
Hello world!

#see if update works
$ touch ./nativeShebang.zkl
$ ./nativeShebang.zsc
Compiled Class(nativeShebang) (0.0 seconds, ??? lines/sec)
Wrote Class(nativeShebang) to ./nativeShebang.zsc
Hello world!
#yep, new binary generated
</pre>
</pre>