Perl 6
Perl 6 Variable Tablet: Revision 103
"Overview"[Perl 6 Tablets] - _Chapter:_ "0:History"[Perl 6 History Tablet], "1:Design"[Perl 6 Language Design Tablet], "2:Basics"[Perl 6 Basics Tablet], "3:Var"[Perl 6 Variable Tablet], "4:Op"[Perl 6 Operator Tablet], *"5:IO"[Perl 6 IO Tablet]*, "6:{}"[Perl 6 Block Tablet], "7:Sub"[Perl 6 Subroutine Tablet], "8:OOP"[Perl 6 OOP Tablet], "9:Rx"[Perl 6 Regex Tablet], "10:Meta"[Perl 6 Meta Tablet]
"Intro"[Perl 6 Intro Tablet] - _Appendices:_ "A:Index"[Perl 6 Index Tablet], "B:Tables"[Perl 6 Lookup Tablet], "C:Cook"[Perl 6 Cookbook Tablet], "D:Delta"[Perl 6 Delta Tablet], "E:Best of"[Best Of Perl 6 Tablet], "F:Links"[Perl 6 Links Tablet]

----

{toc: }

_Herbert Breunung: Inside a shell might be a Perl._

This tablet is about the core stuff of Perl's genesis - reading and writing files, running commands, and gluing parts of the operating environment together in new ways.

^ Interacting with the Terminal

The command terminal (`STDOUT` to Perl 5 programmer, and `$*OUT` in Perl 6) is the primary communication device for a script with its user. Most tutorials begin with writing a line to that black window.

^^ say

.pre
say 'Dear Earthlings!';
.pre

Write text to the standard output (`$*OUT`, called `STDOUT` in Perl 5). This example just prints a "string literal"{link: [Perl 6 Basics Tablet] Quoting}, but any type of value is allowed. *say* adds a line ending, according to the operating system convention. So every time you "say" something, it will start on a fresh line.

You can also give `say` a list of values. Each item will be taken as an expression, that has to evaluated. The results will be combined, just like with *"~"{link: [perl 6 operator tablet] string context}*, and finally the line ending will be added.

.pre
say 'Dear Earthlings, we come from ', $our_home_planet, ', which is ', compute_distance($earth, $our_home_planet), ' parsecs away.';
.pre

But normally this would be written using "double-quoted strings"{link: [Perl 6 Basics Tablet] Quoting}, like:

.pre
say "Dear Earthlings, we come from $our_home_planet, which is { compute_distance($earth, $our_home_planet) } parsecs away.";
.pre

^^ print

Like *say*, just without adding the line ending. Remember it like this: machines *print* explicitly, character by character without pauses. People, on the other hand, speak in sentences or speech balloons, which have to be separated.

^^ printf

It's a `print` that understands also the syntax of "sprintf"{link: [Perl 6 Basics tablet] sprintf}. It's a very lightweight framework for formated output. If you need more, use formats or even a template engine.

.pre
my $num = 22/7;
printf "Even on $our_home_planet, the ratio of a circle's circumference to its diameter is approximately %0.3f\n", $num;
.pre

^^ note

Works like "say"{link: say}, but writes to `$*ERR` (known as `STDERR` in Perl 5). Content sent to `$*ERR` can be redirected separately from normal program output, and is often used for progress reports, extra debugging information, error messages, etc.

^^ warn

Behaves somewhat like `print` to `$*ERR` (see {link: note}). Under the hood, `warn` is a "resumable exception", and its message can be caught and handled farther up the call stack in some way. Normally, once it's handled, Perl will resume running the code just after the `warn` call.

^^ get

Reads one line of input from `$*IN` (called `STDIN` in Perl 5), and strips the line ending. So unlike in Perl 5, you don't have to `chomp` input. All following line-oriented commands (e.g., `prompt`, `lines`) behave that way.

^^ prompt

Performs two-way communication with the terminal. It does an output like `print`, followed by a read like `get`.

.pre
given prompt "Enter geosynchronous orbit (y/N)? " {
when / :i ^ y / { say "Warning, merging into geosync layer." }
default { say "Good, staying out of traffic." }
}
.pre

^^ getc

Reads one character (may be more than one byte) from `$*IN`. Don't try using this to read key-by-key interactively.

^ Files

^^ :X

"file test operators"{link: [Perl 6 Lookup Tablet] Filetest Ops}

^^ slurp

Reads the entire contents of a file as a single string.

.pre
my $contents = slurp 'filename.txt';
.pre

^^ lines

^^ open

^^ read for files

^^ print for files

^^ say for files

^^ open

Synopsis:

> open('new', :w).close

> given open('foo', :w) { # "Anonymous" filehandle
>> .say('Hello, world!');
>> .close
> }

Valid modes are:

r : read

w : write

a : append (Note: you must specify "wa", not just "a")

p : pipe

^^ close

^^ rename for files

^ Directories

^^ $*CWD

^^ dir

^^ mkdir

^^ chdir

^^ chroot

^^ rmdir

^^ rename

^ Sockets

^ Processes

^^ run

^^ runinstead

^^ fork

^^ wait

^^ kill

----

"Overview"[Perl 6 Tablets] - _Chapter:_ "0:History"[Perl 6 History Tablet], "1:Design"[Perl 6 Language Design Tablet], "2:Basics"[Perl 6 Basics Tablet], "3:Var"[Perl 6 Variable Tablet], "4:Op"[Perl 6 Operator Tablet], *"5:IO"[Perl 6 IO Tablet]*, "6:{}"[Perl 6 Block Tablet], "7:Sub"[Perl 6 Subroutine Tablet], "8:OOP"[Perl 6 OOP Tablet], "9:Rx"[Perl 6 Regex Tablet], "10:Meta"[Perl 6 Meta Tablet]
"Intro"[Perl 6 Intro Tablet] - _Appendices:_ "A:Index"[Perl 6 Index Tablet], "B:Tables"[Perl 6 Lookup Tablet], "C:Cook"[Perl 6 Cookbook Tablet], "D:Delta"[Perl 6 Delta Tablet], "E:Best of"[Best Of Perl 6 Tablet], "F:Links"[Perl 6 Links Tablet]

----


Upload Files

Click "Browse" to find the file you want to upload. When you click "Upload file" your file will be uploaded and added to the list of attachments for this page.

Maximum file size: 50MB

 
 
 
File Name Author Date Uploaded Size

Save Page As

Enter a meaningful and distinctive title for your page.

Page Title:

Tip: You'll be able to find this page later by using the title you choose.

Page Already Exists

There is already a page named XXX. Would you like to:

Save with a different name:

Save the page with the name "XXX"

Append your text to the bottom of the existing page named: "XXX"

Upload Files

Click "Browse" to find the file you want to upload. When you click "Add file" this file will be added to the list of attachments for this page, and uploaded when you save the page.

 
 
 
Add Tags

Enter a tag and click "Add tag". The tag will be saved when you save the page.

Tag: 

Suggestions: