Perl-data-types

提供:Dev Guides
移動先:案内検索

Perl-データ型

Perlは緩やかに型付けされた言語であり、プログラムでの使用中にデータの型を指定する必要はありません。 Perlインタープリターは、データ自体のコンテキストに基づいてタイプを選択します。

Perlには、スカラー、スカラーの配列、および連想配列とも呼ばれるスカラーのハッシュの3つの基本データ型があります。 これらのデータ型について少し詳しく説明します。

Sr.No. Types & Description
1

Scalar

スカラーは単純な変数です。 それらの前にはドル記号($)が付いています。 スカラーは、数値、文字列、または参照です。 参照は実際には変数のアドレスであり、これについては次の章で説明します。

2

Arrays

配列は、0で始まる数値インデックスでアクセスするスカラーの順序付きリストです。 それらの前には「アット」記号(@)が付いています。

3

Hashes

ハッシュは、キーを添え字として使用してアクセスするキー/値ペアの順序付けられていないセットです。 これらの前にはパーセント記号(%)が付いています。

数値リテラル

Perlは、すべての数値を符号付き整数または倍精度浮動小数点値として内部に保存します。 数値リテラルは、次の浮動小数点または整数形式のいずれかで指定されています-

Type Value
Integer 1234
Negative integer -100
Floating point 2000
Scientific notation 16.12E14
Hexadecimal 0xffff
Octal 0577

文字列リテラル

文字列は文字の並びです。 通常は、単一引用符( ')または二重引用符( ")で区切られた英数字の値です。 これらは、単一引用符で囲まれた文字列と二重引用符で囲まれた文字列を使用できるUNIXシェル引用符のように機能します。

二重引用符で囲まれた文字列リテラルでは変数の補間が可能ですが、単一引用符で囲まれた文字列ではできません。 バックスラッシュが続く特定の文字があり、特別な意味を持ち、改行(\ n)またはタブ(\ t)のような表現に使用されます。

あなたは二重引用符で囲まれた文字列に改行または次のエスケープシーケンスのいずれかを直接埋め込むことができます-

Escape sequence Meaning
\\ Backslash
\' Single quote
\" Double quote
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\0nn Creates Octal formatted numbers
\xnn Creates Hexideciamal formatted numbers
\cX Controls characters, x may be any character
\u Forces next character to uppercase
\l Forces next character to lowercase
\U Forces all following characters to uppercase
\L Forces all following characters to lowercase
\Q Backslash all following non-alphanumeric characters
\E End \U, \L, or \Q

文字列が一重引用符と二重引用符でどのように動作するかをもう一度見てみましょう。 ここでは、上記の表に記載されている文字列エスケープを使用し、スカラー変数を使用して文字列値を割り当てます。

#!/usr/bin/perl

# This is case of interpolation.
$str = "Welcome to \nfinddevguides.com!";
print "$str\n";

# This is case of non-interpolation.
$str = 'Welcome to \nfinddevguides.com!';
print "$str\n";

# Only W will become upper case.
$str = "\uwelcome to finddevguides.com!";
print "$str\n";

# Whole line will become capital.
$str = "\UWelcome to finddevguides.com!";
print "$str\n";

# A portion of line will become capital.
$str = "Welcome to \Ufinddevguides\E.com!";
print "$str\n";

# Backsalash non alpha-numeric including spaces.
$str = "\QWelcome to finddevguides's family";
print "$str\n";

これは、次の結果を生成します-

Welcome to
finddevguides.com!
Welcome to \nfinddevguides.com!
Welcome to finddevguides.com!
WELCOME TO finddevguides.COM!
Welcome to finddevguides.com!
Welcome\ to\ finddevguides\'s\ family