Print itself

Revision as of 00:34, 11 June 2020 by Thundergnat (talk | contribs) (→‎{{header|Raku}}: Add a Raku example)

Create a program, which prints its source code to the stdout!

Print itself 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.
Related task



Furor

<lang Furor> 1 argv getfile dup sprint free end </lang>

Go

<lang go>package main

import (

   "fmt"
   "io/ioutil"
   "log"
   "os"
   "path"

)

func main() {

   self := path.Base(os.Args[0]) + ".go"
   bytes, err := ioutil.ReadFile(self)
   if err != nil {
       log.Fatal(err)
   }
   fmt.Print(string(bytes))

}</lang>

Output:

Just the invoking line as remainder is, of course, as above.

$ go run self_print.go

Raku

Works with: Rakudo version 2020.05

Not really sure what the point of this task is.

Is is supposed to be a quine? <lang perl6>my &f = {say $^s, $^s.raku;}; f "my \&f = \{say \$^s, \$^s.raku;}; f " </lang>

Or just a program that when executed echoes its source to STDOUT? (Here's probably the simplest valid program that when executed, echoes its source to STDOUT. It is exceptionally short: zero bytes; and when executed echoes zero bytes to STDOUT.)

<lang perl6></lang>

Or are we supposed to demonstrate how to locate the currently executing source code file and incidentally, print it.

<lang perl6>print $*PROGRAM.slurp</lang>

Whatever. Any of these satisfy the rather vague specifications.

REXX

<lang rexx>/*REXX program prints its own multi─line source to the standard output (stdout). */

   do j=1  for sourceline()
   call lineout , sourceline(j)
   end   /*j*/                                  /*stick a fork in it,  we're all done. */</lang>

Wren

<lang ecmascript>import "os" for Process import "io" for File

var args = Process.allArguments System.write(File.read(args[1]))</lang>

Output:

Just the invoking line as remainder is, of course, as above.

$ wren self_print.wren