Hello world/Line printer: Difference between revisions

From Rosetta Code
Content added Content deleted
(AWK, Factor are also incorrect.)
(→‎{{header|UNIX Shell}}: For BSD users, pipe to lpr(1) or use /dev/lpt0 or use /dev/ulpt0.)
Line 171: Line 171:


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
Use ''one'' of the following lines.


<lang bash># Use the default printer queue, with lp(1) or lpr(1).
{{works with|Bourne Shell}}
# 1. The system must have a printer queue.
# 2. The printer queue must understand plain text.
# 3. System V has lp(1). BSD has lpr(1).
# CUPS has both lp(1) and lpr(1).
#
echo 'Hello World!' | lp
echo 'Hello World!' | lpr


# Use a character device.
<lang bash>echo 'Hello World!'|lp</lang>
# 1. The device must understand plain text.

# 2. You must have write permission for the device.
{{works with|POSIX}}
# 3. Some systems have /dev/lp0, /dev/lp1, ...

# 4. BSD has /dev/lpt0, /dev/lpt1 for the parallel ports,
Alternately, there are the character devices <tt>/dev/lp0</tt>, <tt>/dev/lp1</tt>, <tt>/dev/lpN</tt> and so on which correspond to line printers (if there are any attached to the system). Data written to these devices is sent to attached printers.
# and /dev/ulpt0, /dev/ulpt1 for the USB printers.

#
<lang bash>echo 'Hello World' > /dev/lp0</lang>
echo 'Hello World!' >/dev/lp0
echo 'Hello World!' >/dev/lpt0
echo 'Hello World!' >/dev/ulpt0</lang>


{{omit from|PARI/GP}}
{{omit from|PARI/GP}}

Revision as of 20:08, 29 April 2011

Task
Hello world/Line printer
You are encouraged to solve this task according to the task description, using any language you may know.

Cause a line printer attached to the computer to print a line containing the message Hello World!

Note: A line printer is not the same as standard output. A line printer is an older-style printer which prints one line at a time to a continuous ream of paper.

Applesoft BASIC

Assumes a printer card is installed in the Apple II's number 1 expansion slot.

<lang basic> PR#1 PRINT "HELLO WORLD!" </lang>

AutoHotkey

<lang AutoHotkey> Fileappend, Hallo World!, print.txt Run, print "print.txt" </lang>

AWK

This example is incorrect. Please fix the code and remove this message.

Details: Outputs to a regular file named "prn", not a line printer.

<lang AWK> BEGIN { print("Hello World!") >"prn" } </lang>

BASIC

Works with: QBasic
Works with: ZX Spectrum Basic
Works with: Liberty BASIC

<lang qbasic>LPRINT "Hello World!"</lang>

Batch File

<lang dos>ECHO Hello world!>PRN</lang>

C

This example is incorrect. Please fix the code and remove this message.

Details: It prints to standard out rather than to a line printer.

<lang C>#include <stdio.h>

int main() {

  printf("Hello world!\n");
  return 0;

}</lang>

C++

This example is incorrect. Please fix the code and remove this message.

Details: Outputs to stdout

<lang cpp>#include <iostream>

int main() {

   std::cout << "Hello world!" << std::endl; // std::endl provides a portable descriptor for line ending.
   return 0;

}</lang>

Factor

This example is incorrect. Please fix the code and remove this message.

Details: Outputs to stdout, not a line printer.

<lang factor> USE: io IN: hello-world

hello ( -- ) "Hello World!" print ;

MAIN: hello</lang>

when you want a fully working program or in the listener:

<lang factor>"Hello World!" print</lang>

Haskell

<lang haskell> import System.Cmd

cmd = "echo \"Hello World!\" | lpr"

main = system cmd </lang>

Integer BASIC

See Applesoft BASIC.

J

<lang j>require'print' print'Hello world!'</lang>

Perl

Assuming that the line printer is attached to /dev/lp0 <lang perl>open O, ">/dev/lp0"; print O "Hello World!\n"; close O;</lang>

PHP

This example is incorrect. Please fix the code and remove this message.

Details: Outputs to stdout

<lang PHP><?php echo 'Hello world!'; ?></lang>

PicoLisp

<lang PicoLisp>(out '(lpr "-P" "Printer01")

  (prinl "Hello world") )</lang>

PostScript

Technically not really correct as this has to be sent to the printer directly. It will output Hello world, then, though. <lang postscript><</PageSize [595 842]>> setpagedevice  % set page size to DIN A4 /Courier findfont  % use Courier 12 scalefont setfont  % 12 pt 28 802 moveto  % 1 cm from the top and left edges (Hello world) show  % draw the string</lang>

PureBasic

Library: PureLPRINT

<lang PureBasic>MyPrinter$ = LPRINT_GetDefaultPrinter() If LPRINT_OpenPrinter(MyPrinter$)

 If LPRINT_StartDoc("Printing a RC-Task")
   LPRINT_Print(Chr(27) + "E") ; PCL reset for HP Printers
   LPRINT_PrintN("Hello World!")
   LPRINT_NewPage()
   LPRINT_EndDoc()
 EndIf
 LPRINT_ClosePrinter()  

EndIf</lang>

Python

Assuming that the line printer is attached to /dev/lp0 <lang python>lp = open("/dev/lp0") lp.write("Hello World!/n") lp.close()</lang>

REXX

There is no direct way for REXX programs to write to the printer, but
a shell command could be used.

In DOS (or under Windows): <lang rexx> str='Hello World' '@ECHO' str ">PRN" </lang>

Ruby

This example is incorrect. Please fix the code and remove this message.

Details: It prints to standard out rather than to a line printer (which is a peripheral device).

<lang ruby> puts "Hello World" </lang>

Tcl

Unix

<lang tcl>exec lp << "Hello World!"</lang> <lang tcl>set f [open |lp w] puts $f "Hello World!" close $f</lang>

Windows

<lang tcl>set f [open prn w] puts $f "Hello World!" close $f</lang>

UNIX Shell

Use one of the following lines.

<lang bash># Use the default printer queue, with lp(1) or lpr(1).

  1. 1. The system must have a printer queue.
  2. 2. The printer queue must understand plain text.
  3. 3. System V has lp(1). BSD has lpr(1).
  4. CUPS has both lp(1) and lpr(1).

echo 'Hello World!' | lp echo 'Hello World!' | lpr

  1. Use a character device.
  2. 1. The device must understand plain text.
  3. 2. You must have write permission for the device.
  4. 3. Some systems have /dev/lp0, /dev/lp1, ...
  5. 4. BSD has /dev/lpt0, /dev/lpt1 for the parallel ports,
  6. and /dev/ulpt0, /dev/ulpt1 for the USB printers.

echo 'Hello World!' >/dev/lp0 echo 'Hello World!' >/dev/lpt0 echo 'Hello World!' >/dev/ulpt0</lang>