Hello world/Line printer: Difference between revisions
(Fixed PHP examples) |
(I added Salmon code.) |
||
Line 245: | Line 245: | ||
<lang ruby>open("| lpr", "w") { |f| f.puts "Hello World!" }</lang> |
<lang ruby>open("| lpr", "w") { |f| f.puts "Hello World!" }</lang> |
||
=={{header|Salmon}}== |
|||
Assuming /dev/lp0 accesses the printer: |
|||
<lang Salmon>open_output_text_file("/dev/lp0").print("Hello World!");</lang> |
|||
Assuming lpr is a command that prints to a printer: |
|||
<lang Salmon>`echo "Hello World!" | lpr`;</lang> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
Revision as of 03:50, 5 November 2011
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 was an older-style printer which prints one line at a time to a continuous ream of paper. With some systems, a line printer can be any device attached to an appropriate port (such as a parallel port).
Ada
Unix
Assuming that the line printer is attached to /dev/lp0 <lang Ada> with Ada.Text_IO; use Ada.Text_IO;
procedure Print_Line is
Printer : File_Type;
begin
begin Open (Printer, Mode => Out_File, Name => "/dev/lp0"); exception when others => Put_Line ("Unable to open printer."); return; end;
Set_Output (Printer); Put_Line ("Hello World!"); Close (Printer);
end Print_Line; </lang>
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
<lang AWK> BEGIN { print("Hello World!") >"/dev/lp0" } </lang>
BASIC
<lang qbasic>LPRINT "Hello World!"</lang>
Batch File
<lang dos>ECHO Hello world!>PRN</lang>
BBC BASIC
<lang bbcbasic> prn% = OPENOUT("PRN:")
PRINT #prn%, "Hello World!" CLOSE #prn%</lang>
C
Unix
Assuming that the line printer is attached to /dev/lp0 <lang C>#include <stdio.h>
int main() {
FILE *lp; lp = fopen("/dev/lp0","w"); fprintf(lp,"Hello world!\n"); fclose(lp); return 0;
}</lang>
C++
<lang cpp>#include <iostream>
- include <fstream>
int main(){
std::ofstream lprFile; lprFile.open( "/dev/lp0" ); lprFile << "Hello World\n"; lprFile.close(); return 0;
}</lang>
Delphi
<lang Delphi>program Project1;
{$APPTYPE CONSOLE}
uses Printers;
var
lPrinterAsTextFile: TextFile;
begin
AssignPrn(lPrinterAsTextFile); Rewrite(lPrinterAsTextFile); Writeln(lPrinterAsTextFile, 'Hello World!'); CloseFile(lPrinterAsTextFile);
end.</lang>
Factor
Prints through Unix "lpr" command.
<lang factor>( scratchpad ) USE: io.encodings.utf8 ( scratchpad ) USE: io.launcher ( scratchpad ) "lpr" utf8 [ "Hello World!" print ] with-process-writer</lang>
Groovy
<lang groovy>new File('/dev/lp0').write('Hello World\n') </lang>
GUISS
<lang guiss>Start,Programs,Accessories,Notepad,Type:Goodbye World[pling], Menu:File,Print,Button:OK</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>
JavaScript
<lang javascript>// This example runs on Node.js var fs = require('fs'); // Assuming lp is at /dev/lp0 var lp = fs.openSync('/dev/lp0', 'w'); fs.writeSync(lp, 'Hello, world!\n'); fs.close(lp);</lang>
Locomotive Basic
<lang locobasic>10 PRINT #8, "Hello World!"</lang>
OCaml
Assuming that the line printer is attached to /dev/lp0 <lang ocaml>let () =
let oc = open_out "/dev/lp0" in output_string oc "Hello world!\n"; close_out oc ;;</lang>
OpenEdge/Progress
<lang progress>OUTPUT TO PRINTER. PUT UNFORMATTED "Hello world!" SKIP. OUTPUT CLOSE.</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>
Perl 6
<lang perl6>given open '>', '/dev/lp0' {
.say('Hello, World!'); .close;
}</lang>
PHP
<lang PHP><?php file_put_contents('/dev/lp0', 'Hello world!'); ?></lang>
<lang PHP><?php fclose(STDOUT); $STDOUT = fopen('/dev/lp0', 'a'); echo 'Hello world!'; ?></lang>
PicoLisp
<lang PicoLisp>(out '(lpr "-P" "Printer01")
(prinl "Hello world") )</lang>
PL/I
<lang PL/I> hello: procedure options (main);
put ('Hello world.');
end hello; </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
<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
Assumes that lpr
command reaches printer.
<lang ruby>open("| lpr", "w") { |f| f.puts "Hello World!" }</lang>
Salmon
Assuming /dev/lp0 accesses the printer:
<lang Salmon>open_output_text_file("/dev/lp0").print("Hello World!");</lang>
Assuming lpr is a command that prints to a printer: <lang Salmon>`echo "Hello World!" | lpr`;</lang>
Scheme
Unix
Assuming device is attached to lp0
<lang scheme>(call-with-output-file "/dev/lp0" (lambda (printer) (write "Hello World!" printer)))</lang>
SNOBOL4
In SNOBOL4, variables can be associated with input and output files. Assigning a value to an output-associated variable also writes it to the associated output file. (Likewise, accessing a variable associated with an input file returns as its value the next record from the associated input file.) By default, the variable "input" is associated with standard input, and the variable "output" is associated with standard output.
<lang SNOBOL4> output = "Hello, world."</lang>
You can associate the variable "print" with lpt1 (the default local printer port) using the output() function:
<lang SNOBOL4> output(.print,25,"lpt1")
print = "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. 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.
- 1. The device must understand plain text.
- 2. You must have write permission for the device.
- 3. Some systems have /dev/lp0, /dev/lp1, ...
- 4. BSD has /dev/lpt0, /dev/lpt1, ... for the parallel ports;
- and /dev/ulpt0, /dev/ulpt1, ... for the USB printers.
- Note that intermingling can occur if two processes write to the device at the
- same time. Using the print spooler method above avoids this problem,
echo 'Hello World!' >/dev/lp0 echo 'Hello World!' >/dev/lpt0 echo 'Hello World!' >/dev/ulpt0</lang>
- Programming Tasks
- Solutions by Programming Task
- Ada
- Applesoft BASIC
- AutoHotkey
- AWK
- BASIC
- Batch File
- BBC BASIC
- C
- C++
- Delphi
- Factor
- Groovy
- GUISS
- Haskell
- Integer BASIC
- J
- JavaScript
- Locomotive Basic
- OCaml
- OpenEdge/Progress
- Perl
- Perl 6
- PHP
- PicoLisp
- PL/I
- PostScript
- PureBasic
- PureLPRINT
- Python
- REXX
- Ruby
- Salmon
- Scheme
- SNOBOL4
- Tcl
- UNIX Shell
- PARI/GP/Omit
- ML/I/Omit
- Printer