Hello world/Standard error: Difference between revisions
→{{header|JavaScript}}: Added Node.js example |
Added CoffeeScript |
||
Line 120: | Line 120: | ||
stop run. |
stop run. |
||
</lang> |
</lang> |
||
=={{header|CoffeeScript}}== |
|||
{{trans|JavaScript}} |
|||
{{works with|Node.js}} |
|||
<lang coffeescript>console.warn "Goodbye, World!"</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
Revision as of 02:17, 4 June 2011
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
A common practice in computing is to send error messages to a different output stream than normal text console messages. The normal messages print to what is called "standard output" or "standard out". The error messages print to "standard error". This separation can be used to redirect error messages to a different place than normal messages.
Show how to print a message to standard error by printing "Goodbye, World!" on that stream.
4DOS Batch
<lang 4dos>echoerr Goodbye, World!</lang>
Ada
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
procedure Goodbye_World is begin
Put_Line (Standard_Error, "Goodbye, World!");
end Goodbye_World;</lang>
ALGOL 68
The procedures print and printf output to stand out, whereas put and putf can output to any open file, including stand error.
<lang algol68>main:(
put(stand error, ("Goodbye, World!", new line))
)</lang> Output:
Goodbye, World!
Argile
<lang Argile>use std eprint "Goodbye, World!"</lang> or <lang Argile>use std eprintf "Goodbye, World!\n"</lang> or <lang Argile>use std fprintf stderr "Goodbye, World!\n"</lang>
AutoHotkey
requires AutoHotkey_N implementation. <lang autohotkey>; c:\> autohotkey.exe stderr.ahk 2> error.txt FileAppend, Goodbye`, World!, stderr ; requires AutoHotkey_N</lang>
AWK
<lang awk>BEGIN {
print "Goodbye, World!"| "cat 1>&2"
}</lang>
Batch File
<lang dos>1>&2 echo Goodbye, World!</lang>
The redirection operator 1>&2
causes all output on stream 1 (standard out) to be redirected to stream 2 (standard error). The redirection can be moved to the end of the line, too.
BASIC
ZX Spectrum Basic
On the ZX Spectrum, standard error is on stream 1:
<lang zxbasic> 10 PRINT #1;"Goodbye, World!" 20 PAUSE 5: REM allow time for the user to see the error message </lang>
BBC BASIC
The program must be compiled as a console application for this to work. <lang bbcbasic> STD_ERROR_HANDLE = -12
SYS "GetStdHandle", STD_ERROR_HANDLE TO @hfile%(1) PRINT #13, "Goodbye, World!" QUIT</lang>
C
Unlike puts(), fputs() does not append a terminal newline. <lang c>#include <stdio.h>
int main() { fprintf(stderr, "Goodbye, "); fputs("World!\n", stderr);
return 0; }</lang>
C#
<lang csharp>static class StdErr {
static void Main(string[] args) { Console.Error.WriteLine("Goodbye, World!"); }
}</lang>
C++
<lang cpp>#include <iostream>
using std::cerr; using std::endl;
int main () {
cerr << "Goodbye, World!" << endl;
return cerr.bad();
}</lang>
Clojure
<lang lisp>(binding [*out* *err*]
(println "Goodbye, world!"))</lang>
COBOL
Using fixed format.
<lang cobol> program-id. ehello. procedure division. display "Goodbye, world!" upon syserr. stop run. </lang>
CoffeeScript
<lang coffeescript>console.warn "Goodbye, World!"</lang>
Common Lisp
<lang lisp>(format *error-output* "Goodbye, world!~%")</lang>
D
<lang D>import tango.io.Stdout;
void main () {
Stderr("Goodbye, World!").newline;
}</lang>
Delphi
<lang Delphi>program Project1;
{$APPTYPE CONSOLE}
begin
WriteLn(ErrOutput, 'Goodbye, World!');
end.</lang>
E
<lang e>stderr.println("Goodbye, World!")</lang>
Euphoria
<lang Euphoria>puts(2,"Goodbye, world!\n") -- 2 means output to 'standard error'</lang>
Factor
Start Factor in a terminal for this. <lang factor>error-stream get [ "Goodbye, World! bbl, crashing" print flush ] with-output-stream*</lang>
Fantom
<lang fantom> class Main {
public static Void main () { Env.cur.err.printLine ("Goodbye, World!") }
} </lang>
Forth
<lang forth>outfile-id
stderr to outfile-id ." Goodbye, World!" cr
to outfile-id</lang>
Fortran
Normally standard error is associated with the unit 0 but this could be different for different vendors. Therefore since Fortran 2003 there's an intrinsic module which defines the parameter ERROR_UNIT.
<lang fortran>program StdErr
! Fortran 2003 use iso_fortran_env
! in case there's no module iso_fortran_env ... !integer, parameter :: ERROR_UNIT = 0 write (ERROR_UNIT, *) "Goodbye, World!"
end program StdErr</lang>
F#
<lang fsharp>eprintfn "%s" "Goodbye, World!"</lang> or you can use the .Net classes <lang fsharp>System.Console.Error.WriteLine("Goodbye, World!");</lang>
Go
Built in println now goes to stderr. <lang go>package main func main() { println("Goodbye, World!") }</lang>
Groovy
<lang groovy>System.err.println("Goodbye, World!")</lang>
Haskell
<lang haskell>import System.IO hPutStrLn stderr "Goodbye, World!"</lang>
Icon and Unicon
<lang icon>procedure main()
write(&errout, "Goodbye World" )
end</lang>
J
<lang j>stderr =: 1!:2&4 stderr 'Goodbye, World!'</lang>
Java
<lang java>public class Err{
public static void main(String[] args){ System.err.println("Goodbye, World!"); }
}</lang>
JavaScript
and only with cscript.exe
<lang javascript>WScript.StdErr.WriteLine("Goodbye, World!");</lang>
<lang javascript>console.warn("Goodbye, World!")</lang>
Lua
<lang lua>io.stderr:write("Goodbye, World!\n")</lang>
MATLAB
<lang MATLAB>error 'Goodbye, World!'</lang>
Metafont
Metafont has no a real way to send a text to the standard output/error nor to a file. Anyway it exists the errmessage
command which will output an error message and prompt the user for action (suspending the interpretation of the source).
<lang metafont>errmessage "Error"; message "...going on..."; % if the user decides to go on and not to stop
% the program because of the error.</lang>
Modula-2
<lang modula2>MODULE HelloErr; IMPORT StdError;
BEGIN
StdError.WriteString('Goodbye, World!'); StdError.WriteLn
END HelloErr.</lang>
Modula-3
<lang modula3>MODULE Stderr EXPORTS Main;
IMPORT Wr, Stdio;
BEGIN
Wr.PutText(Stdio.stderr, "Goodbye, World!\n");
END Stderr.</lang>
Objective-C
In Objective-C one can use the standard C library and the stderr as in the C language; nonetheless a common way to output to stderr for logging purpose and/or error notification is the NSLog function, that works almost like fprintf(stderr, "..."), save for the fact that the format string is an NSString object, and it also prepends a timestamp.
<lang objc>#import <Foundation/Foundation.h>
int main() {
fprintf(stderr, "Goodbye, World!\n"); fputs("Goodbye, World!\n", stderr); NSLog(@"Goodbye, World!"); return 0;
}</lang>
OCaml
<lang ocaml>prerr_endline "Goodbye, World!"; (* this is how you print a string with newline to stderr *) Printf.eprintf "Goodbye, World!\n"; (* this is how you would use printf with stderr *)</lang>
we can also use the out_channel stderr:
<lang ocaml>output_string stderr "Goodbye, World!\n"; Printf.fprintf stderr "Goodbye, World!\n";</lang>
finally the Unix module also provides unbuffered write functions:
<lang ocaml>let msg = "Goodbye, World!\n" in ignore(Unix.write Unix.stderr msg 0 (String.length msg)) ;;</lang>
Oz
<lang oz>functor import Application System define
{System.showError "Goodbye, World!"} {Application.exit 0}
end</lang>
PARI/GP
<lang parigp>error("Goodbye, World!")</lang>
Pascal
<lang pascal>program byeworld;
begin
writeln(StdErr, 'Goodbye, World!');
end.</lang>
Perl
<lang perl>warn "Goodbye, World!\n";</lang>
Or:
<lang perl>print STDERR "Goodbye, World!\n";</lang>
Perl 6
<lang perl6>note "Goodbye, World!";</lang>
PHP
<lang php>fprintf(STDERR, "Goodbye, World!\n");</lang>
PicoLisp
<lang PicoLisp>(out 2 (prinl "Goodbye, World!"))</lang>
PL/I
<lang PL/I>display ('Goodbye, World');</lang>
PostScript
<lang postscript>(%stderr) (w) file dup (Goodbye, World! ) writestring closefile</lang>
PowerShell
Since PowerShell has a slightly different system of pipes and streams (to facilitate easy usage from a host application) the standard Write-Error cmdlet is mainly for sending annotated error messages to the host: <lang powershell>Write-Error "Goodbye, World!"</lang> Note that this outputs more than just the message because behind the scenes it is an uncaught exception:
Write-Error "Goodbye, World!" : Goodbye, World! + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
To accurately reproduce the behavior of other languages one has to resort to .NET in this case: <lang powershell>[Console]::Error.WriteLine("Goodbye, World!")</lang>
PureBasic
ConsoleError() writes the message string (plus a newline) to the standard error output of current program.
Standard error output can be used in conjunction with ReadProgramError() to reads a line from an other programs error output (stderr).
<lang PureBasic>ConsoleError("Goodbye, World!")</lang>
Python
<lang python>import sys
print >> sys.stderr, "Goodbye, World!"</lang>
<lang python>import sys
print("Goodbye, World!", file=sys.stderr)</lang>
R
<lang R>cat("Goodbye, World!", file=stderr())</lang>
Retro
<lang Retro>with files' "Goodbye, World!" tempString dup getLength "/dev/stderr" spew</lang>
REXX
Version 1. <lang rexx> say 'Goodbye, World!' </lang> Version 2. <lang rexx> say "Goodbye, World!" </lang> Version 3. <lang rexx> call lineout ,"Goodbye, World!" </lang> Version 4. <lang rexx> call charout ,"Goodbye, World!" </lang>
Ruby
<lang ruby>$stderr.puts "Goodbye, World!"</lang>
The following also works, unless you have disabled warnings (ruby command line option "-W0" or set $VERBOSE=nil
)
<lang ruby>warn "Goodbye, World!"</lang>
Sather
<lang sather>class MAIN is
main is #ERR + "Hello World!\n"; end;
end;</lang>
Scala
<lang scala>System.err.println("Goodbye, World!")</lang>
Scheme
<lang scheme>(error "Goodbye, World!")</lang>
sed
Requires /dev/stderr
<lang sed>#n 1 { s/.*/Goodbye, World!/w /dev/stderr }</lang>
This program requires at least 1 line of input. It changes the first line to "Goodbye, World!" and then prints the first line to standard error. It reads and ignores the remaining lines.
Test output: <lang bash>$ echo a | sed -f error.sed >/dev/null Goodbye, World!</lang>
Slate
<lang slate>inform: 'Goodbye, World!' &target: DebugConsole.</lang>
SNOBOL4
<lang snobol4> terminal = "Error"
output = "Normal text"
end</lang>
Standard ML
<lang sml>TextIO.output (TextIO.stdErr, "Goodbye, World!\n")</lang>
Tcl
<lang tcl>puts stderr "Goodbye, World!"</lang>
TUSCRIPT
<lang tuscript> $$ MODE TUSCRIPT PRINT/ERROR "hello world" text="goodbye world" PRINT/ERROR text </lang> Output:
@@@@@@@@ hello world @@@@@@@@ @@@@@@@@ goodbye world @@@@@@@@
UNIX Shell
<lang bash>echo "Goodbye, World!" > /dev/stderr</lang>
<lang bash>echo "Goodbye, World!" >&2</lang>
UnixPipes
<lang bash>echo "Goodbye, World!" 1>&2</lang>
X86 Assembly
This is known to work on Linux, it may or may not work on other Unix-like systems
Note that it is only 2 characters different from the Assembly example on User Output - text
Prints "Goodbye, World!" to stderr (and there is probably an even simpler version): <lang assembly>section .data msg db 'Goodbye, World!', 0AH len equ $-msg
section .text global _start _start: mov edx, len
mov ecx, msg mov ebx, 2 mov eax, 4 int 80h
mov ebx, 1 mov eax, 1 int 80h</lang>
- Programming Tasks
- Basic language learning
- Selection/Short Circuit/Console Program Basics
- 4DOS Batch
- Ada
- ALGOL 68
- Argile
- AutoHotkey
- AWK
- Batch File
- BASIC
- ZX Spectrum Basic
- BBC BASIC
- C
- C sharp
- C++
- Clojure
- COBOL
- CoffeeScript
- Common Lisp
- D
- Tango
- Delphi
- E
- Euphoria
- Factor
- Fantom
- Forth
- Fortran
- F Sharp
- Go
- Groovy
- Haskell
- Icon
- Unicon
- J
- Java
- JavaScript
- Lua
- MATLAB
- Metafont
- Modula-2
- Modula-3
- Objective-C
- OCaml
- Oz
- PARI/GP
- Pascal
- Perl
- Perl 6
- PHP
- PicoLisp
- PL/I
- PostScript
- PowerShell
- PureBasic
- Python
- R
- Retro
- REXX
- Ruby
- Sather
- Scala
- Scheme
- Sed
- Slate
- SNOBOL4
- Standard ML
- Tcl
- TUSCRIPT
- UNIX Shell
- UnixPipes
- X86 Assembly
- Applesoft BASIC/Omit
- Bc/Omit
- Brainf***/Omit
- Dc/Omit
- Integer BASIC/Omit
- TI-83 BASIC/Omit
- TI-89 BASIC/Omit
- Unlambda/Omit
- Streams