Perl6 特殊変数チートシート - Dynamic variables

こんばんは :whale2: 

今日は3歩進むたびに忘れてしまうPerl6の特殊変数について書いておこうと思います。

## `$*ARGFILES`

> $*ARGFILES Magic command-line input handle.

```pl:argfiles.pl6
use v6;

$*ARGFILES.perl.say; #=> IO::Handle.new(:path(Any),:chomp)

# 一行ずつ読み込み
for $*ARGFILES.lines -> $line {
  say "$line";
}

# 一気に読み込み
# say $*ARGFILES.slurp;
```

```bash:USAGE
$ perl6 argfiles.pl6 読みたいファイル
```

* [class IO::Handle - Perl 6 Documentation](http://doc.perl6.org/type/IO::Handle)
* [Input/Output - Perl 6 Documentation](http://doc.perl6.org/language/io)
* [Perl6でのファイル操作 - Qiita](http://qiita.com/syohex/items/0a7782920968ab105ba0)

## `@*ARGS`

> @*ARGS Arguments from the command line.

```pl:args.pl6
use v6;

say @*ARGS.WHAT;  #=> (Array)
say @*ARGS;       #=> [a b c d e]

say @*ARGS.perl;  #=> ["a", "b", "c", "d", "e"]
```

```bash:USAGE
$ perl6 args.pl6 a b c d e
```

* [class Array - Perl 6 Documentation](http://doc.perl6.org/type/Array)

## `$*IN`

> $*IN Standard input filehandle, AKA stdin

```pl:in.pl6
use v6;

say $*IN.perl;   #=> IO::Handle.new(:path(IO::Special.new(what => "<STDIN>")),:chomp)
say $*IN.path;   #=> IO::Special.new(what => "<STDIN>")
say $*IN.chomp;  #=> True

for $*IN.lines -> $line {
 say "$line";
}
```

```bash:USAGE
$ perl6 in.pl6
入力
…

$ cat 読みたいファイル | perl6 in.pl6
```

## `$*OUT`

> $*OUT Standard output filehandle, AKA stdout

```php:out.pl6
use v6;

say $*OUT.perl;   #=> IO::Handle.new(:path(IO::Special.new(what => "<STDOUT>")),:chomp)
say $*OUT.path;   #=> IO::Special.new(what => "<STDOUT>")
say $*OUT.chomp;  #=> True

$*OUT.say( q:to/ヒアドキュメント/ );

あなたがドなら私はレ！
　あなたがレなら私はミ！

　　私は上を行くよ！

ヒアドキュメント

# 普通は$*OUT省略してこんな風に書くと思う。
# say "高鳴る！私の胸のBPM！"
```

```bash:USAGE
$ perl6 out.pl6

$ perl6 out.pl6 > 書きたいファイル
```

* [role IO - Perl 6 Documentation](http://doc.perl6.org/type/IO)

## `$*ERR`

> $*ERR Standard error filehandle, AKA stderr

```pl:err.pl6
use v6;

say $*ERR.perl;   #=> IO::Handle.new(:path(IO::Special.new(what => "<STDERR>")),:chomp)
say $*ERR.path;   #=> IO::Special.new(what => "<STDERR>")
say $*ERR.chomp;  #=> True

$*ERR.say( '穏やかじゃない！' );

# 普通標準エラー出力するなら note() を使うかも。
# note "この穏やかさ穏やかじゃないわね"
```

```bash:USAGE
$ perl6 err.pl6 > /dev/null
穏やかじゃない！
```

## `$*REPO`

> $*REPO A variable holding information about modules installed/loaded

```pl:repo.pl6
use v6;

say $*REPO;
say $*REPO.perl;
say $*REPO.id;
say $*REPO.path-spec;
say $*REPO.loaded;
say $*REPO.repo-chain;
```

## `$*TZ`

> $*TZ The system's local timezone.

```pl:tz.pl6
use v6;

say $*TZ;      #=> 32400
say $*TZ.perl; #=> 32400

say $*TZ.WHAT; #=> (Int)
```

## `$*CWD`

> $*CWD The Current Working Directory.

```pl:cwd.pl6
use v6;

say $*CWD;       #=> "/Users/kujira".IO
say $*CWD.path;  #=> /Users/kujira
say $*CWD.perl;  #=> "/Users/kujira".IO(:SPEC(IO::Spec::Unix),:CWD("/Users/kujira"))
```

## `$*KERNEL`

> $*KERNEL Which kernel am I compiled for?

```pl:kernel.pl6
use v6;

say $*KERNEL;            #=> darwin (15.2.0)
say $*KERNEL.release;    #=> Darwin Kernel Version 15.2.0: Fri Nov 13 19:56:56 PST 2015; root:xnu-3248.20.55~2/RELEASE_X86_64
say $*KERNEL.name;       #=> darwin
say $*KERNEL.auth;       #=> unknown
say $*KERNEL.version;    #=> v15.2.0
say $*KERNEL.signature;  #=> (Blob)
say $*KERNEL.desc;       #=> (Str)

say $*KERNEL.perl;        #=> Kernel.new(release => Str, name => "darwin", auth => "unknown", version => Version.new('15.2.0'), signature => Blob, desc => Str)
say $*KERNEL.WHAT;        #=> (Kernel)
```

## `$*DISTRO`

> $*DISTRO Which OS distribution am I compiling under?

```pl:distro.pl6
use v6;

say $*DISTRO;           #=> macosx (10.11.2)

say $*DISTRO.name;      #=> macosx
say $*DISTRO.is-win;    #=> False
say $*DISTRO.version;   #=> v10.11.2

say $*DISTRO.path-sep;  #=> :
say $*DISTRO.auth;      #=> Apple Computer, Inc.
say $*DISTRO.desc;      #=> 2016-01-17T01:48:03.261407+09:00
say $*DISTRO.release;   #=> 15C50
say $*DISTRO.signature; #=> (Blob)

say $*DISTRO.gist;      #=> macosx (10.11.2)
say $*DISTRO.Str;       #=> macosx
say $*DISTRO.perl;      #=> Distro.new(release => "15C50", is-win => Bool::False, path-sep => ":", name => "macosx", auth => "Apple Computer, Inc.", version => Version.new('10.11.2'), signature => Blob, desc => "2016-01-17T01:48:47.273804+09:00")
```

* [Perl6 で windows かどうか判定したい - tokuhirom's blog](http://blog.64p.org/entry/2015/09/05/225701)
* [DISTRO.t](https://github.com/perl6/roast/blob/master/S02-magicals/DISTRO.t)


## `$*VM`

> $*VM Which virtual machine am I compiling under?

```pl:vm.pl6
use v6;

say $*VM;         #=> moar (2015.12)

say $*VM.config;
say $*VM.perl;
```

## `$*PERL`

> $*PERL Which Perl am I compiled for?

```pl:perl.pl6
use v6;

say $*PERL;          #=> Perl 6 (6.c)
say $*PERL.compiler; #=> rakudo (2015.12)

say $*PERL.perl;     #=> Perl.new(compiler => Compiler.new(id => "AEB5E66886F036F5AF7448E587F49EB233F6F7F5.1451295526.86961", release => "", codename => "", name => "rakudo", auth => "The Perl Foundation", version => Version.new('2015.12'), signature => Blob, desc => Str), name => "Perl 6", auth => "The Perl Foundation", version => Version.new('6.c'), signature => Blob, desc => Str)
```

## `$*PID`

> $*PID Process ID of the current process.

```pl:pid.pl6
use v6;

say $*PID;      #=> 35480
say $*PID.perl; #=> 35480
say $*PID.WHAT; #=> (Int)
```

## `$*PROGRAM-NAME`

> $*PROGRAM-NAME Path to the current executable as it was entered on the command line, or C<-e> if perl was invoked with the -e flag.

```pl:program-name.pl6
use v6;

say $*PROGRAM-NAME;
say $*PROGRAM-NAME.perl;
say $*PROGRAM-NAME.IO.basename;
```

## `$*PROGRAM`

> $*PROGRAM Location (in the form of an C<IO::Path> object) of the Perl program being executed.

```pl:program.pl6
use v6;

say $*PROGRAM;        #=> "/Users/kujira/program.pl6".IO
say $*PROGRAM.Str;    #=> program.pl6

say $*PROGRAM.perl;   #=> "program.pl6".IO(:SPEC(IO::Spec::Unix),:CWD("/Users/kujira"))

say $*PROGRAM.SPEC;   #=> (Unix)
say $*PROGRAM.CWD;    #=> /Users/kujira

say $*PROGRAM.WHAT;   #=> (Path)
```

## `$*EXECUTABLE`

> $*EXECUTABLE Absolute path of the perl executable that is currently running.

```pl:executable.pl6
use v6;

say $*EXECUTABLE;           #=> "/usr/local/bin/perl6".IO
say $*EXECUTABLE.Str;       #=> /usr/local/bin/perl6
say $*EXECUTABLE.basename;  #=> perl6

say $*EXECUTABLE.WHAT;      #=> (Path)

say $*EXECUTABLE.perl;      #=> "/usr/local/bin/perl6".IO(:SPEC(IO::Spec::Unix))
say $*EXECUTABLE.SPEC;      #=> (Unix)
```

## `$*EXECUTABLE-NAME`

> $*EXECUTABLE-NAME The name of the perl executable that is currently running. (e.g. perl6-p, perl6-m, Niecza.exe) Favor $*EXECUTABLE because it is not guaranteed that the perl executable is in PATH.

```pl:executable-name.pl6
use v6;

say $*EXECUTABLE-NAME;       #=> perl6
say $*EXECUTABLE-NAME.WHAT;  #=> (Str)
```

## `$*USER`

> $*USER The user that is running the program. It is an object that evaluates to "username (uid)". It will evaluate to the username only if treated as a string and the numeric user id if treated as a number.

```pl:user.pl6
use v6;

say $*USER;      #=> kujira (801)

say +$*USER;     #=> 801
say ~$*USER;     #=> kujira

say $*USER.perl; #=> IdName.new
```

## `$*GROUP`

> $*GROUP The primary group of the user who is running the program. It is an object that evaluates to "groupname (gid)". It will evaluate to the groupname only if treated as a string and the numeric group id if treated as a number.

```pl:group.pl6
use v6;

say $*GROUP;       #=> whale (0)

say ~$*GROUP;      #=> whale
say +$*GROUP;      #=> 0

say $*GROUP.perl;  #=> IdName.new
```

## `$*HOME`

> $*HOME An L<IO::Path> object representing the "home directory" of the user that is running the program. If the "home directory" cannot be determined it will be L<Nil>

```pl:home.pl6
use v6;

say $*HOME;       #=> "/Users/kujira".IO

say $*HOME.CWD;   #=> /Users/kujira
say $*HOME.SPEC;  #=> (Unix)
say $*HOME.WHAT;  #=> (Path)

say $*HOME.perl;  #=> "/Users/kujira".IO(:SPEC(IO::Spec::Unix),:CWD("/Users/kujira"))
```

* [class IO::Path - Perl 6 Documentation](http://doc.perl6.org/type/IO::Path)

## `$*SPEC`

> $*SPEC The appropriate L<IO::Spec> sub-class for the platform that the program is running on.

```pl:spec.pl6
use v6;

say $*SPEC;          #=> (Unix)
say $*SPEC.perl;     #=> IO::Spec::Unix
say $*SPEC.path;     #=> (/usr/local/Cellar/rakudo-star/2015.12/share/perl6/site/bin /usr/local/sbin /usr/local/bin /usr/bin /bin /usr/sbin /sbin)
say $*SPEC.tmpdir;   #=> "/var/folders/9v/wr31l2zj78x1nw58jgljq_9w0000gn/T".IO
say $*SPEC.dir-sep;  #=> /
say $*SPEC.curdir;   #=> .
say $*SPEC.updir;    #=> ..
say $*SPEC.curupdir; #=> none(., ..)
say $*SPEC.rootdir;  #=> /
say $*SPEC.devnull;  #=> /dev/null
```

* [class IO::Spec - Perl 6 Documentation](http://doc.perl6.org/type/IO::Spec)
* [class IO::Spec::QNX - Perl 6 Documentation](http://doc.perl6.org/type/IO::Spec::QNX)
* [class IO::Spec::Unix - Perl 6 Documentation](http://doc.perl6.org/type/IO::Spec::Unix)
* [class IO::Spec::Win32 - Perl 6 Documentation](http://doc.perl6.org/type/IO::Spec::Win32)
* [class IO::Spec::Cygwin - Perl 6 Documentation](http://doc.perl6.org/type/IO::Spec::Cygwin)

## 参考と注釈

* [Variables - Perl 6 Documentation] (http://doc.perl6.org/language/variables#Dynamic_variables)
* [Perl6を学ぶ時に参考になるページ - Qiita](http://qiita.com/B73W56H84/items/54bb7de2f3183d8c49ba)
* [QiitaのシンタックスハイライトがまだPerl6に対応してないみたいどうしよう - Qiita](http://qiita.com/B73W56H84/items/633b0c5fda663e313b59)