LoginSignup
1
3

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-01-16

こんばんは :whale2:

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

$*ARGFILES

$*ARGFILES Magic command-line input handle.

argfiles.pl6
use v6;

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

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

# 一気に読み込み
# say $*ARGFILES.slurp;
USAGE
$ perl6 argfiles.pl6 読みたいファイル

@*ARGS

@*ARGS Arguments from the command line.

args.pl6
use v6;

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

say @*ARGS.perl;  #=> ["a", "b", "c", "d", "e"]
USAGE
$ perl6 args.pl6 a b c d e

$*IN

$*IN Standard input filehandle, AKA stdin

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";
}
USAGE
$ perl6 in.pl6
入力
…

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

$*OUT

$*OUT Standard output filehandle, AKA stdout

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!"
USAGE
$ perl6 out.pl6

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

$*ERR

$*ERR Standard error filehandle, AKA stderr

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 "この穏やかさ穏やかじゃないわね"
USAGE
$ perl6 err.pl6 > /dev/null
穏やかじゃない!

$*REPO

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

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.

tz.pl6
use v6;

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

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

$*CWD

$*CWD The Current Working Directory.

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?

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?

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")

$*VM

$*VM Which virtual machine am I compiling under?

vm.pl6
use v6;

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

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

$*PERL

$*PERL Which Perl am I compiled for?

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.

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.

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 CIO::Path object) of the Perl program being executed.

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.

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.

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.

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.

group.pl6
use v6;

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

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

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

$*HOME

$*HOME An LIO::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

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"))

$*SPEC

$*SPEC The appropriate LIO::Spec sub-class for the platform that the program is running on.

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

参考と注釈

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3