1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Perl6 スカラー

1
Posted at

最初に

正式リリースの前にPerl6の予習をしようというものです。
今回はスカラー変数について。

Perl6の環境構築についてはこちら。
Perl6導入とHello, world!

スカラー

スカラーとは

スカラーは1個の単純な値を持つ変数で、先頭に半角ダラー($)をつけて表します。

use v6;

my $camelCase;
my $snake_case;
my $chain-case; # 変数名にハイフン(-)も使えます。
my #`(use utf8;が無くても) $変数名に日本語も使えます;

初期化、代入と型

動的型付けされます。

use v6;

my $a;        # Any型
my $b = 1;    # Int型
my $c = 3.14; # Rat型
my $d = True; # Bool型
my $e = ''; # Str型

$b.^name.say; #=> Int
$b = $e;        # Int型だった $b にStr型の $e を代入
$b.WHAT.say;  #=> (Str)

宣言時に型指定もできます。

use v6;

my Str $文字列;

$文字列 = 'やぁ!';
$文字列.say; #=> やぁ!

# Int型を代入しようとすると…
$文字列 = 1; # エラー!
# => Type check failed in assignment to '$文字列'; expected 'Str' but got 'Int'

# Str型にキャストして代入しましょう。
$文字列 = ~1;
$文字列.perl.say; #=> "1"

参考や注釈

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?