Hello world/Standard error: Difference between revisions
Underscore (talk | contribs) (Added Common Lisp.) |
(Slate implementation) |
||
Line 164: | Line 164: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<lang ruby>$stderr.puts("Goodbye, World!")</lang> |
<lang ruby>$stderr.puts("Goodbye, World!")</lang> |
||
=={{header!Slate}}== |
|||
<lang slate>inform: 'Goodbye, World!' &resource: DebugConsole.</lang> |
|||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
Revision as of 17:56, 5 June 2009
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.
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.
main:( put(stand error, ("Goodbye, World!", new line)) )
Output:
Goodbye, World!
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 0;
}</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
stderr.println("Goodbye, World!")
Forth
outfile-id stderr to outfile-id ." Goodbye, World!" cr to outfile-id
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>
Haskell
<lang haskell> import System.IO
hPutStrLn stderr "Goodbye, World!"</lang>
Java
<lang java>public class Err{
public static void main(String[] args){ System.err.println("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>
Perl
<lang perl>print STDERR "Goodbye, World!\n";</lang>
PHP
<lang php>fprintf(STDERR, "Goodbye, World!\n");</lang>
Python
<lang python>import sys
print >> sys.stderr, "Goodbye, World!"</lang>
<lang python>import sys
print("Goodbye, World!", file=sys.stderr)</lang>
Ruby
<lang ruby>$stderr.puts("Goodbye, World!")</lang>
Template:Header!Slate
<lang slate>inform: 'Goodbye, World!' &resource: DebugConsole.</lang>
Standard ML
<lang sml>TextIO.output (TextIO.stdErr, "Goodbye, World!\n")</lang>
Tcl
<lang tcl>puts stderr "Goodbye, World!"</lang>
UNIX Shell
echo "Goodbye, World!" > /dev/stderr
UnixPipes
echo "Goodbye, World!" 1>&2