Hello world/Standard error: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|C++}}: Fixed return in case of error writing to stream.)
(added Go)
Line 128: Line 128:
or you can use the .Net classes
or you can use the .Net classes
<lang fsharp>System.Console.Error.WriteLine("Goodbye, World!");</lang>
<lang fsharp>System.Console.Error.WriteLine("Goodbye, World!");</lang>

=={{header|Go}}==
<lang go>package main

import "fmt"
import "os"

func main() { fmt.Fprintln(os.Stderr, "Goodbye, World!") }</lang>


=={{header|Haskell}}==
=={{header|Haskell}}==

Revision as of 07:40, 5 June 2010

Task
Hello world/Standard error
You are encouraged to solve this task according to the task description, using any language you may know.
Hello world/Standard error is part of Short Circuit's Console Program Basics selection.

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.

Works with: ALGOL 68 version Standard - no extensions to language used
Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386 - note that printf and putf were not ported into ELLA's libraries.

<lang algol68>main:(

 put(stand error, ("Goodbye, World!", new line))

)</lang> Output: <lang algol68>Goodbye, World!</lang>

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>

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>

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

Works with: GNU 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>

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

Works with: JScript

and only with cscript.exe

<lang javascript>WScript.StdErr.WriteLine("Goodbye, World!");</lang>

Lua

<lang lua>io.stderr:write("Goodbye, World!\n")</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

Works with: GNUstep
Works with: Cocoa

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>

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

Works with: Python version 2.x

<lang python>import sys

print >> sys.stderr, "Goodbye, World!"</lang>

Works with: Python version 3.x

<lang python>import sys

print("Goodbye, World!", file=sys.stderr)</lang>

R

<lang R>cat("Goodbye, World!", file=stderr())</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>

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

Works with: nasm version 2.05.01

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>