Hello world/Standard error: Difference between revisions
No edit summary |
add language: Retro |
||
Line 293: | Line 293: | ||
=={{header|R}}== |
=={{header|R}}== |
||
<lang R>cat("Goodbye, World!", file=stderr())</lang> |
<lang R>cat("Goodbye, World!", file=stderr())</lang> |
||
=={{header|Retro}}== |
|||
<lang Retro>with files' |
|||
"Goodbye, World!" tempString dup getLength "/dev/stderr" spew</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
Revision as of 16:55, 12 October 2010
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>
Bash
<lang bash>echo "Goodbye, World!" >&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.
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>
Common Lisp
<lang lisp>(format *error-output* "Goodbye, world!~%")</lang>
D
<lang D>import tango.io.Stdout;
void main () {
Stderr("Goodbye, World!").newline;
}</lang>
E
<lang e>stderr.println("Goodbye, World!")</lang>
Factor
Start Factor in a terminal for this. <lang factor>error-stream get [ "Goodbye, World! bbl, crashing" print flush ] with-output-stream*</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
<lang go>package main
import "fmt" import "os"
func main() { fmt.Fprintln(os.Stderr, "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
Icon
<lang icon>procedure main()
write(&errout, "Goodbye World" )
end</lang>
Unicon
This Icon solution works in Unicon.
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>
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-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 string is a NSString object.
<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>
Perl
<lang perl>warn "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>
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>
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>
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>
UNIX Shell
<lang bash>echo "Goodbye, World!" > /dev/stderr</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
- Bash
- Batch File
- C
- C sharp
- C++
- Clojure
- COBOL
- Common Lisp
- D
- Tango
- E
- Factor
- Forth
- Fortran
- F Sharp
- Go
- Groovy
- Haskell
- Icon
- Unicon
- J
- Java
- JavaScript
- Lua
- MATLAB
- Metafont
- Modula-3
- Objective-C
- OCaml
- Oz
- Perl
- Perl 6
- PHP
- PicoLisp
- PL/I
- PowerShell
- PureBasic
- Python
- R
- Retro
- Ruby
- Sather
- Scala
- Scheme
- Slate
- SNOBOL4
- Standard ML
- Tcl
- UNIX Shell
- UnixPipes
- X86 Assembly
- TI-83 BASIC/Omit
- TI-89 BASIC/Omit
- Unlambda/Omit