Perl/関数のソースを表示
←
Perl/関数
ナビゲーションに移動
検索に移動
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループに属する利用者のみが実行できます:
登録利用者
。
このページのソースの閲覧やコピーができます。
<noinclude> {{Nav}} {{Pathnav|プログラミング|Perl}} </noinclude> <includeonly> =関数= {{先頭に戻る}} </includeonly> == 関数とサブルーチン == Perlにおいて、「[[#組込み関数|関数]]」と「[[#サブルーチン|サブルーチン]]」という用語は異なる意味で使われます。まず、「関数」という言葉は主にPerlが提供する組み込み機能、つまり'''[[#組込み関数|組込み関数]]'''を指します。たとえば、<code>print</code>や<code>chomp</code>、<code>length</code>といった関数がこれに該当します。これらはPerlの言語仕様にあらかじめ用意されたキーワードであり、特別な処理が施されています。たとえば、<code>print</code>はリスト全体を引数として受け取り、文法的にも他の部分と衝突することなく優先的に認識されます。一方で、組み込み関数は<code>\&</code>を使ってコードリファレンスを取得することができず、別のサブルーチンや無名サブルーチンを使ってラップする必要があります。 これに対して、「[[#サブルーチン|サブルーチン]]」は、ユーザーが<code>sub</code>キーワードを用いて定義するコードブロックを指します。サブルーチンは自由に作成・変更でき、自分で名前を付けて定義することが可能です。たとえば、<code>sub greet { return "Hello, World\n"; }</code>のようにして、独自のサブルーチンを定義できます。そして、このサブルーチンは<code>\&greet</code>のように[[#コードリファレンス|コードリファレンス]]として取得できるため、他のサブルーチンに引数として渡したり、後から実行したりすることができます。 呼び出し方についても違いがあります。たとえば、組み込み関数とサブルーチンのどちらも引数がない場合には括弧<code>()</code>を省略できます。しかし、サブルーチンの場合には<code>()</code>を省略すると、構文が曖昧になる可能性があるため、可読性の観点から括弧を付けることが推奨されます。一方で、組み込み関数はキーワードとして特別に扱われるため、括弧を省略しても曖昧さが生じにくい設計になっています。 組み込み関数とサブルーチンにはこうした違いがありますが、両者を混在させて使う際には注意が必要です。特に、サブルーチンと同名の組み込み関数を定義すると、意図せず組み込み関数が優先される場合があります。これを避けるためには、名前の付け方や呼び出し方に注意する必要があります。 また、組み込み関数を他のサブルーチンに引数として渡したい場合には、直接渡すことはできないため、無名サブルーチンを用いてラップするのが一般的です。このように、Perlでは「関数」と「サブルーチン」が明確に区別され、それぞれ特有のルールや扱い方があることを理解することが重要です。 == 組込み関数 == Perlの言語コアで予め定義されている関数のことを「組込み関数」と呼びます。<code>[[#print|print]]</code>、<code>[[#length|length]]</code>、<code>[[#substr|substr]]</code>などの一般的な関数から、<code>[[#my|my]]</code>、<code>[[#use|use]]</code>、<code>[[#do|do]]</code>などのキーワード的な構文要素に至るまで、広くPerl自身に組み込まれた関数がこれに当てはまります。 === 基本的な関数 === ==== print関数 ==== ;機能:print関数は、引数で与えられた文字列や文字列のリストを標準出力に出力します。引数が与えられなかったときは <code>$_</code> が出力されます。 ;[https://paiza.io/projects/Isiw6BqruLhVNGQFCFCSQQ?language=perl 例]:<syntaxhighlight lang=perl copy> use v5.30.0; use warnings; print "Hello, World\n"; print "Hello, Perl\n" </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> Hello, World Hello, Perl </syntaxhighlight> :print関数は、行末で改行しないので、もし改行をしたい場合には明示的にエスケープシーケンス <code>\n</code> を文字列の末尾に加えます。 ==== say関数 ==== Perl 5.10 から導入されたsay 関数は、行末で改行を行います。これで、都度 <code>\n</code> を文字列末に記述する手間が省けます。 ;[https://paiza.io/projects/kCCvuvfgVSM9IGX84QE1cg?language=perl 組込み関数 say]:<syntaxhighlight lang=perl highlight="6-8" copy> use strict; use warnings; use utf8; binmode STDOUT,":encoding(UTF-8)"; use feature "say"; use feature ':5.10'; use v5.10; say "Hello"; say "Yes!"; say "Boodbye"; my $message = "こんにちは"; say $message; </syntaxhighlight> : say を使うには、6-8 行目の use 宣言のいずれか1つが必要です。 ::<syntaxhighlight lang=perl copy> use feature "say"; use feature ':5.10'; use v5.10; </syntaxhighlight> :#<syntaxhighlight lang=perl copy> use feature "say"; </syntaxhighlight> :#:say を名指しで有効化しています。お勧めです。 :##<syntaxhighlight lang=perl copy> use feature qw(say switch); </syntaxhighlight> :##: の様に2つ以上を列挙することもできます。 :#<syntaxhighlight lang=perl copy> use feature ':5.10'; </syntaxhighlight> :#: バージョン 5.10 以降の機能を全て有効にします。手早く動かすためにはいいのですが過剰です。 :#<syntaxhighlight lang=perl copy> use v5.10; </syntaxhighlight> :#: 意味的には上と同じですが、より簡素です。多分一番多く使われています。 ;CORE<nowiki>::</nowiki>say:<syntaxhighlight lang=perl highlight="6-8" copy> #!/usr/bin/perl use strict; use warnings; CORE::say "Hello world!"; </syntaxhighlight> : CORE<nowiki>::</nowiki>を前置するとプラグマを使わずに say関数を使うことができます。 : ワンライナーや書き捨てのスクリプトに向いています。 : CORE はPerlコアルーチンの名前空間です。 ==== 文字列に変数や式を埋込む ==== Perlでは、文字列の中に変数や式を埋め込むことができ、テンプレート言語であるかのような使いかたが出来ます。 :length は文字列の長さを返します。 ;[https://paiza.io/projects/dTI0m8Arb3XFoZUXcGo4kA?language=perl 文字列に変数や式を埋込む]:<syntaxhighlight lang=perl copy> use v5.30.0; use warnings; my $x = "aeiou"; my $tmp = length $x; say "length \"$x\" -> $tmp"; say "length \"aeiou\" -> @{[length 'aeiou']}"; say qq(length "aeiou" -> @{[length 'aeiou']}); </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> length "aeiou" -> 5 length "aeiou" -> 5 length "aeiou" -> 5 </syntaxhighlight> :この様に、<code>”</code>(ダブルクォーテーションマーク)に囲まれた文字列の中では <code>$変数</code> で式の値が、<code>@{[式]}</code> で式の値が文字列に埋込まれます。 ::厳密に解説するには、スカラーコンテキストとリストコンテキストの説明が必要なのですが、リファレンスなどの説明が必須なので、機会を見て一括して解説します。 : qw// 演算子を使うと、変数や式が展開する文字列の中で :<code>”</code>(ダブルクォーテーションマーク)ではなく、<code>’</code>(シングルクォーテーションマーク)で囲まれた文字列では、変数や式は展開されません。 ==== 数学関数 ==== ===== 基本的な数学関数 ===== 平方根などの数学計算をする関数が用意されています。 ;[https://paiza.io/projects/AjzHuhdjMmEhQSMbw_Ufsg?language=perl 最小のピタゴラス数]:<syntaxhighlight lang=perl copy> use v5.20.0; use warnings; say "sqrt(3**2 + 4**2) --> @{[sqrt(3**2 + 4**2)]}"; use POSIX "hypot"; say "hypot(3, 4) --> @{[ hypot(3, 4) ]}" </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> sqrt(3**2 + 4**2) --> 5 hypot(3, 4) --> 5 </syntaxhighlight> : Perlの組込み関数 sqrt を使って自乗和の平方根を求めています。 : 自乗は結果がオーバーフローあるいはアンダーフローを起こす可能性があるので、対策された hypot を使うのが定石です。 : ですが、Perlの組込み関数にもMathモジュールにも hypot はなく、POSIXモジュールにあります。 : この場合、<code>use POSIX "hypot";</code>ではなく<code>use POSIX;</code>で充分なのですが、POSIXからhypotを持ってきている意外性を伝えるため明示しました。 : 呼出し側で、<code>POSIX::hypot(3, 4)</code> とするのも刺激的ですが、複数箇所あると鬱陶しいので use 側で対処しました。 ====== hypot.pl ====== 桁あふれ対策と可変引数に対応したPerl版hypotの例。 ;[https://paiza.io/projects/s4oMcB0qOxQCF6zqsJf_2A?language=perl hypot.pl]:<syntaxhighlight lang=perl copy> use v5.30.0; use warnings; use POSIX; sub hypot { my ( $max, $s ) = ( 0, 0 ); # 最大値を決定する for my $n (@_) { next if $n == 0; return $n if $n != $n; # for NaN my $arg = abs($n); return $arg if $arg == "Inf";# for Inf $max = $arg if $max < $arg; } # 最大値に基づいてスケーリングしつつ加算 for my $n (@_) { next if $n == 0; $s += ($n / $max) ** 2; # 最大値でスケーリングして加算 } # 結果を返す return $max * sqrt($s); } if ( $0 eq __FILE__ ) { foreach my $i ( -1075 .. -1073, -540 .. -538, 0 .. 2, 508 .. 511, 1021 .. 1024 ) { my $j = 2**$i; my ( $n, $m ) = ( 3 * $j, 4 * $j ); say "$i: @{[ 5 * $j ]} @{[ sqrt($n*$n + $m*$m) ]} @{[ ::hypot($n, $m) ]} @{[ POSIX::hypot($n, $m) ]}"; } } </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> -1075: 0 0 0 0 -1074: 2.47032822920623e-323 0 2.47032822920623e-323 2.47032822920623e-323 -1073: 4.94065645841247e-323 0 4.94065645841247e-323 4.94065645841247e-323 -540: 1.38922421842817e-162 0 1.38922421842817e-162 1.38922421842817e-162 -539: 2.77844843685635e-162 3.14345556940526e-162 2.77844843685635e-162 2.77844843685635e-162 -538: 5.55689687371269e-162 5.44462475754526e-162 5.55689687371269e-162 5.55689687371269e-162 0: 5 5 5 5 1: 10 10 10 10 2: 20 20 20 20 508: 4.18993997810706e+153 4.18993997810706e+153 4.18993997810706e+153 4.18993997810706e+153 509: 8.37987995621412e+153 8.37987995621412e+153 8.37987995621412e+153 8.37987995621412e+153 510: 1.67597599124282e+154 Inf 1.67597599124282e+154 1.67597599124282e+154 511: 3.35195198248565e+154 Inf 3.35195198248565e+154 3.35195198248565e+154 1021: 1.12355820928895e+308 Inf 1.12355820928895e+308 1.12355820928895e+308 1022: Inf Inf Inf Inf 1023: Inf Inf Inf Inf 1024: Inf Inf Inf Inf </syntaxhighlight> : Perlには、Cの isnan() や isfinite() に相当する関数がないので、それぞれ <code>$n != $n</code> と <code>abs($n) == "Inf"</code> としました。 :: POSIXモジュールにはisfinite関数があるので、それを使えばよいのですが、POSIX::hypotの代替実装なので利用を見送りました。 ===== 三角関数など ===== sin,cos は組込み関数にありますが、tan, acos など他の三角関数や円周率(pi)を使用するには、use宣言を使って Math::Trigモジュールから導入します。 ;[https://paiza.io/projects/GyO8xRXjKsGkXzuvl2L9wQ?language=perl 余弦関数と逆余弦関数]:<syntaxhighlight lang=perl highlight=4 copy> use 5.30.0; use warnings; use Math::Trig qw(pi acos); say "cos(pi) -> cos(@{[pi]}) -> @{[cos(pi)]}"; say "acos(-1) -> @{[acos(-1)]}" </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> cos(pi) -> cos(3.14159265358979) -> -1 acos(-1) -> 3.14159265358979 </syntaxhighlight> :円周率は、Math::Trigモジュールを導入すると使えるようになりますが、<code>$pi</code>ではなく <code>pi</code>です。 :: 文字列中で参照する場合は <code>"@{[pi]}"</code> となります。 : Perlの三角関数の角度の単位は多くのプログラミング言語同様ラジアン(弧度法)です。 : 正弦sinと余弦cosはPerlの言語コアで定義されていますが、正接tanはMath::Trigモジュールで定義されています。 :: Math::Trigモジュールでは、piなどの定数や他の三角関数関連の諸関数が定義されています。 {{See also|[https://perldoc.perl.org/Math::Trig perldoc Math::Trig]}} ==== 日付時刻関係の関数 ==== 現在の日時や時刻などを表すには、time関数およびlocaltime関数を使います。 ;[https://paiza.io/projects/4-FKQ--QuCFTcHN321xcsA?language=perl エポックからの秒数と、ローカル時刻]:<syntaxhighlight lang=perl highlight=4 copy> use v5.30; use warnings; use utf8; binmode STDOUT,":encoding(UTF-8)"; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time()); say "time() -> @{[time()]}"; say "いまは、@{[1900 + $year]} 年 @{[1 + $mon]} 月 $mday 日 $hour 時 $min 分 $sec 秒です。"; use POSIX "strftime"; say strftime "%Y/%m/%d %H:%M:%S", localtime(); say strftime "%c", localtime(); </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> time() -> 1668851859 いまは、2022 年 11 月 19 日 9 時 57 分 39 秒です。 2022/11/19 09:57:39 Sat 19 Nov 2022 09:57:39 AM UTC </syntaxhighlight> ;説明 :time関数は、エポック(1970年1月1日0時0分0秒(UTC) )からの通算秒を返します。 :localtime関数は、エポックからの通算秒形式の引数を、年月日時分秒の要素に分解しリストで返します。 ::localtime関数は、引数を省略すると time()が仮定されるので、この例での引数は冗長です。 : localtimeが返すリストを操作するには1900を足したり月数の補正をしたり面倒です(よく間違えます)。 : POSIXモジュールの strftime を使うと、Cのstrftime()と同じ(正確にはPOSIXと同じ)書式化文字列がつかえ可読性も向上します。使いましょう。 : DateTimeモジュールもあるのですが、Perl流のオブジェクト指向の構文で書かれているので、直感的とは言い難いコードになります。使うなとまでは言いません。 ==== split関数 ==== split関数には、与えられたパターンで文字列を区切り、リストで返します。 ;[https://paiza.io/projects/8JO5ecVl3w4IKNg0tncUmg?language=perl split]:<syntaxhighlight lang=perl highlight=6 copy> use v5.30; use warnings; use utf8; binmode STDOUT,":encoding(UTF-8)"; my @list = split(/ /, '睦月 如月 弥生 卯月 皐月 水無月 文月 葉月 長月 神無月 霧月 師走'); for (my $i = 0; $i <= $#list; $i++){ say qq(@{[$i+1]}月: $list[$i]); } </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> 1月: 睦月 2月: 如月 3月: 弥生 4月: 卯月 5月: 皐月 6月: 水無月 7月: 文月 8月: 葉月 9月: 長月 10月: 神無月 11月: 霧月 12月: 師走 </syntaxhighlight> == サブルーチン == {{Main|[https://perldoc.perl.org/5.36.0/perlsub perlsub(en)]|[https://perldoc.jp/docs/perl/5.36.0/perlsub.pod perlsub(ja)]}} Perlでは、ユーザーが定義する関数のことをサブルーチン( ''subroutine'' )と呼び、キーワード<code>sub</code>を使い定義します。 === シンプルなサブルーチンの定義と呼出し === サブルーチンの定義と呼出しは、説明することがほとんどないほど簡単です。 ;[https://paiza.io/projects/ahyDWYUS_WdFUG1H1TAKbw?language=perl シンプルなサブルーチンの定義と呼出し]:<syntaxhighlight lang=perl line highlight="4-6,8-10,12,13,15,16" copy> use v5.30.0; use warnings; sub world { say "Hello, World"; } sub perl { say "Hello, Perl"; } &world; &perl; world; perl; </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> Hello, World Hello, Perl </syntaxhighlight> : 4-6がサブルーチンworldの定義 : 8-10がサブルーチンperlの定義 :: 見たままです : 12,15 がサブルーチンworldの呼出し : 13,16 がサブルーチンperlの呼出し :: 見たままですが、<code>&</code>が前置されていなくても、されていても同じというのは釈然としません。 :: この <code>&</code> は、組込み関数では前置できません。 :: というわけで、<code>&</code> を関数呼出しで前置するのは、「組込み関数ではなくサブルーチンを呼んでいます」という意味になります。 :: また、<code>&</code> を省略すると[[#サブルーチンの宣言|サブルーチンの宣言]]より前に、サブルーチンを呼出すことはできません。 === サブルーチン宣言 === サブルーチンの定義より先にサブルーチンを呼出す必要があることがあります(典型的には、お互いに呼び合う関数)。 この場合は、呼出ごとに <code>&</code> を前置するか、サブルーチン宣言をサブルーチン呼出の前にします。 ;[https://paiza.io/projects/YCbleN8uFaa1BNzXrHB9mQ?language=perl サブルーチン宣言]:<syntaxhighlight lang=perl line highlight="4,5,7,10" copy> use v5.30.0; use warnings; &world; &perl; sub world; world; sub perl; perl; sub world { say "Hello, World"; } sub perl { say "Hello, Perl"; } </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> Hello, World Hello, Perl Hello, World Hello, Perl </syntaxhighlight> : 4,5は & を前置しているので、宣言がなくてもサブルーチンとわかる。 : 7,10がサブルーチン宣言で、サブルーチン定義の前方参照を解決します。 === グローバル変数を使ったサブルーチンの振る舞いの変更 === 前出の例は、ほとんど同じ内容のサブルーチンを2つ用意しましたが、1つにまとめてみましょう。 ;[https://paiza.io/projects/MBw8XjvoxM7yMMXkbd1CdQ?language=perl グローバル変数を使ったサブルーチンの振る舞いの変更]:<syntaxhighlight lang=perl line highlight="5,8,12,14" copy> use v5.30.0; no strict; use warnings; $who = "WHO!"; sub hello { say "Hello, $who"; } &hello; $who = "world"; &hello; $who = "Perl"; &hello;</syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> Hello, WHO! Hello, world Hello, Perl </syntaxhighlight> : グローバル変数 <var>$who</var> を使ってメッセージの後半を変えています。 : この方法はこの方法で動きますし、かつてのFORTRANやBASICは、まさにこのようにグローバル変数でサブルーチンをコントロールしていました。 : しかし、2行目の<code>no strict;</code>で明示的に strict を無効にしなければエラーが出るほど、グローバル変数の使用は'''推奨されない'''方法です。 === 引数を使ったサブルーチンの振る舞いの変更 === 前出の例は、グローバル変数を使っていましたが、グローバル変数はデーターフロー的なスパゲティーコードに直結するので、引数を使ってスマートに実装してみましょう。 ;[https://paiza.io/projects/NvuWuRBObB3oqD2kq1iFnQ?language=perl 引数を使ったサブルーチンの振る舞いの変更]:<syntaxhighlight lang=perl line highlight="5,6" copy> use v5.30.0; use warnings; sub hello { my $who = shift; $who //= "WHO?!"; say "Hello, $who"; } &hello(); &hello("world"); &hello("Perl"); </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> Hello, WHO?! Hello, world Hello, Perl </syntaxhighlight> :;引数の受け取り:<syntaxhighlight lang=perl line start=4 copy> my $who = shift; </syntaxhighlight> ::Perlのサブルーチンの引数は、名前を持った仮引数ではなく特殊変数 <var>@_</var> に配列として渡されます。 :: 第一引数が <code>$_[0]</code> となります。 :: 半ば定型文なのですが、引数を左から順に読む動作が <code>shift</code>(shift @_ の意味) と符合するので、それらしい名前(この場合は <var>$who</var>)の変数を宣言し <code>shift</code> で初期化するコードが良く見られます。 :: キーワード <code>my</code> を前置して宣言した変数は、「'''レキシカル変数'''」となり、サブルーチン(この場合は hello)を抜けると参照できなくなり、もう一度同じサブルーチンを呼んでも、もう違う値になってます(非永続的なレキシカルスコープ)。 :;ディフォルト引数:<syntaxhighlight lang=perl line start=5 copy> $who //= "WHO?!"; </syntaxhighlight> ::Perlには、ディフォルト引数の構文はなかったので、引数が渡されなかった場合の既定値(ディフォルト)を指定するには、このようなイディオムになります。 この @_ による引数の受渡しは、Perlでは約20年に渡って使われてきましたが、他のプログラミング言語のように名前付きの仮引数が欲しいとの要望は根強く、シグネチャーとしてv5.20.0から'''実験的'''な機能として実装されています。 {{See also|[[#シグネチャー]]}} === 戻値と再帰 === ここまでで、引数を受取りサブルーチンの振舞いを変えることができるようになりました。 次に、「値を返す手段」が問題になります。 グローバル変数を使って値を返せそうですが「データーフロー的なスパゲティーコード」になるのでサブルーチンの「戻値」を使ってみましょう。 ==== 戻値を返すサブルーチン ==== いままでのサブルーチンは値を返しませんでしたが、Perlのサブルーチンは値を1つ返すことができます。 ;[https://paiza.io/projects/NBXcY50TNs_cU1xGQcEOmg?language=perl 戻値を返すサブルーチン]:<syntaxhighlight lang=perl line highlight="6,11" copy> use strict; use warnings; sub add { my ($x, $y) = @_; return $x + $y; } print("add(12, 9) -> @{[add(12, 9)]}\n"); print("add(1.2, 0.9) -> @{[add(1.2, 0.9)]}\n"); print("add(123, '89') -> @{[add(123, '89')]}\n"); </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> add(12, 9) -> 21 add(1.2, 0.9) -> 2.1 add(123, '89') -> 212 </syntaxhighlight> :;戻値を返す文:<syntaxhighlight lang=perl line start=6 copy> return $x + $y; </syntaxhighlight> ::Perlのサブルーチンの戻値を返す場合は :;return:<syntaxhighlight lang=perl copy> return 式 ; </syntaxhighlight> ::での「式」の値が返ります。 :もし return のないサブルーチンの戻値を参照すると、サブルーチンで最後に評価した式の値がかえります。このため ::<syntaxhighlight lang=perl line start=6 copy> return $x + $y; </syntaxhighlight> ::は ::<syntaxhighlight lang=perl line start=6 copy> $x + $y; </syntaxhighlight> ::と同じです。 ::: Perl の <code>;</code> は、Cのように式を文にするのではなく、式と式を区切るデリミターなので最後の式の後に <code>;</code> は不要です。 :戻値とは関係ありませんが、 :;文字列が数値に自動変換される:<syntaxhighlight lang=perl line start=11 copy> print("add(123, '89') -> @{[add(123, '89')]}\n"); </syntaxhighlight> ::が、何事もなかったかのように :::<syntaxhighlight lang=text> add(123, '89') -> 212 </syntaxhighlight> ::となるように、数値が期待されす文脈に数値に変換できる文字列が来ると、自動的に数値に変換され演算されます。 :::Perlのこの暗黙の変換は、エポックからの通算秒が桁上りしたときなどに発現する凶悪なバグの原因になってきました。 ==== 再帰的呼出し ==== 引数と戻値が手に入ったので、再帰的呼出しを行うサブルーチンを書いてみます。 ===== 整数の冪乗 ===== 整数の累乗を返すサブルーチン pow を書いてみました。 ;[https://paiza.io/projects/it2hilVOZ9TGu_lmM68elw?language=perl 整数の冪乗]:<syntaxhighlight lang=perl line highlight="9" copy> use v5.30.0; use warnings; sub pow { my ($n, $m) = @_; return "Domain error" if $m < 0; return 1 if $m == 0; return $n if $m == 1; return $n * &pow($n, $m - 1); } say "pow(2, $_) -> @{[ pow(2, $_) ]}" foreach -1..3; </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> pow(2, -1) -> Domain error pow(2, 0) -> 1 pow(2, 1) -> 2 pow(2, 2) -> 4 pow(2, 3) -> 8 </syntaxhighlight> : 9 で、pow自身を指数を1減らして呼んでいます。 : <code>$n, $m</code> が重複使用されているように見えますが、<code>my</code>がついているので、再帰レベルが1つ下るごとに別のインスタンスが生成されています。 ===== 整数を3桁ごとにカンマで区切って表示する ===== 整数を3桁ごとにカンマで区切って表示するサブルーチン comma3 を書いてみました。 ;[https://paiza.io/projects/urtCz3eFlaG7tRvYLm1Qfw?language=perl 整数を3桁ごとにカンマで区切って表示する]:<syntaxhighlight lang=perl line highlight="6,8" copy> use v5.30.0; use warnings; sub comma3 { my $n = shift; return "-" . comma3(-$n) if $n < 0; my ($num, $rem) = (int($n / 1000), $n % 1000); return comma3($num) . sprintf ",%3d", $rem if $num; return sprintf "%d", $rem } say comma3 $_ foreach qw( 123456789 -999999 0 1 12 123 1234 ) </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> 123,456,789 -999,999 0 1 12 123 1,234 </syntaxhighlight> ===== フィボナッチ数列とメモ化とベンチマーク ===== 再帰で必ず取上げられる[[W:フィボナッチ数列|フィボナッチ数列]]とメモ化を題材に、ベンチマークテストを行ってみようと思います。 ;[https://paiza.io/projects/tndgcBdq34woengCngWc0w?language=perl フィボナッチ数列とメモ化とベンチマーク]:<syntaxhighlight lang=perl line highlight="20" copy> use v5.30.0; use warnings; sub fibonacci { my $n = shift; return $n if $n == 0; return $n if $n == 1; return fibonacci($n - 2) + fibonacci($n - 1) } sub fibonacci_norec { my $n = shift; my ($x, $y) = (1, 0); ($x, $y) = ($y, $x + $y) foreach 1..$n; return $y } sub fibonacci_memorization { my $n = shift; state @table = (0, 1); return $table[$n] if defined $table[$n]; return $table[$n] = fibonacci($n - 2) + fibonacci($n - 1) } use Benchmark qw/timethese cmpthese/; my $i = 16; cmpthese timethese(2 ** 10, { "再帰" => sub { fibonacci($_) foreach 1..$i }, "非再帰" => sub { fibonacci_norec($_) foreach 1..$i }, "メモ化" => sub { fibonacci_memorization($_) foreach 1..$i }, }); </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> Benchmark: timing 1024 iterations of メモ化, 再帰, 非再帰... メモ化: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU) (warning: too few iterations for a reliable count) 再帰: 2 wallclock secs ( 1.58 usr + 0.00 sys = 1.58 CPU) @ 648.10/s (n=1024) 非再帰: 0 wallclock secs ( 0.01 usr + 0.00 sys = 0.01 CPU) @ 102400.00/s (n=1024) (warning: too few iterations for a reliable count) Rate 再帰 非再帰 メモ化 再帰 648/s -- -99% -100% 非再帰 102400/s 15700% -- -100% メモ化 1023999999999999872/s 157999999999999968% 1000000000000001% -- </syntaxhighlight> : fibonacci は、素朴な再帰版のフィボナッチ数列です。 : fibonacci_norec は、非再帰版のフィボナッチ数列です。 : fibonacci_memorization は、メモ化を施した再帰版のフィボナッチ数列です。 :: 20行目の <code>state @table = (0, 1);</code>は、非揮発性のレキシカルスコープ変数の宣言で、 my と違い最初しか初期化されず、再び同じサブルーチンを呼ばれたときには前の値を憶えています。またサブルーチンの外から参照する方法はありません。 : メモ化は、一度計算した答えを記憶して次からは記憶から答える戦略なので、ベンチマークに有利です。 :: メモ化を行うアルゴリズムと行なわないアルゴリズムでは、ベンチマークのような繰り返しに関する感受性が違います。繰返し回数に対し線形に時間が増えないアルゴリズムはメモ化を行っている可能性があるので、ループの底で使われるのには適していますが、頻度の低い使い方の場合、性能が予想より悪い可能性があります。 ::: このことから、実際のプログラムのプロファイル結果とベンチマークの結果の傾向の比較も重要になります。 ===== 無名再帰 ===== サブルーチン自身へのリファレンス __SUB__ を使うと無名関数の再帰ができます。 ;[https://paiza.io/projects/it2hilVOZ9TGu_lmM68elw?language=perl 整数の冪乗]:<syntaxhighlight lang=perl line copy> use v5.30.0; # 階乗 n! say (sub { my $n = shift; return $n == 0 ? $n : $n == 1 ? $n : $n * __SUB__->( $n - 1 ); }->(7)); </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> 5040 </syntaxhighlight> === シグネチャー === 前出の例で、引数を使ってサブルーチンの振る舞いを変えることができました。 機能的には充足しているのですが、<u>名前付きの仮引数</u>を望む声は以前からあり、Perl 5.20.0 から「実験的」( ''experimental'' )なシグネチャーの実装が行なわれ、Perl 5.26.0 からコア機能の1つとなりました。 ;[https://paiza.io/projects/9iJ8toU2PS78IlvlqtcFow?language=perl シグネチャー]:<syntaxhighlight lang=perl line highlight="3,4,6" copy> # !/usr/bin/perl use v5.30; use feature 'signatures'; no warnings "experimental::signatures"; sub hello($who = "WHO?!") { say "Hello, $who"; } &hello(); &hello("world"); &hello("Perl"); </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> Hello, WHO?! Hello, world Hello, Perl </syntaxhighlight> :;シグネチャー:<syntaxhighlight lang=perl line start=6 copy> sub hello($who = "WHO?!") { </syntaxhighlight> ::名前を持った仮引数が使えるようになりました。 ::'''ディフォルト引数'''にも対応しています。 :Perl 5.36.0 からは、signatures は experimental を卒業したので ::<syntaxhighlight lang=perl line copy> use v5.30; use feature 'signatures'; no warnings "experimental::signatures"; </syntaxhighlight> :は ::<syntaxhighlight lang=perl line highlight="3,4,6" copy> use v5.36.0; </syntaxhighlight> : とできます(使用している処理系が、v5.30.0以降の場合に限ります)。 === プロトタイプ === ==== ラムダ抽象 ==== sort のように、コードブロックを引数とするサブルーチンを考えてみましょう。 ;[https://paiza.io/projects/OtxUGCrI2TXyqXxZsWWHDw?language=perl 例]:<syntaxhighlight lang=perl line highlight="3,8,11" copy> use v5.30.0; sub bis(&) { my $cbr = shift; $cbr->(); $cbr->() } bis { say 'Hello, world!' }; my $i = 0; bis { $i++ }; say $i;</syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> Hello, world! Hello, world! 2 </syntaxhighlight> : 与えられたコードブロックを2回実行するサブルーチンです。 : 3行目の<code>sub bis(&)</code>の<code>&</code>はラムダ抽象です。 ==== map を模倣 ==== 組込み関数 map を模倣したサブルーチン mapx を実装します。 ;[https://paiza.io/projects/GWnq66NY0nfz5jDrvjP3pQ?language=perl 例]:<syntaxhighlight lang=perl line copy> use v5.30.0; sub map(&@) { my ( $cbr, @ary ) = @_; my @result; push @result, $cbr->( local $a = $_ ) foreach @ary; return @result; } say main::map { 2 * $_ } ( 1, 2, 3 ); say main::map { 2 * $a } ( 1, 2, 3 ); say CORE::map { 2 * $_ } ( 1, 2, 3 ); say main::map { $_ x 2 } qw(a b c); say main::map { $a x 2 } qw(a b c); say CORE::map { $_ x 2 } qw(a b c); </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> 246 246 246 aabbcc aabbcc aabbcc </syntaxhighlight> : 組込み関数 sort の様に、<var>$a</var> でコードブロックに引数を渡すこともできるようにしました。 :: local で宣言しているので、スコープは foreach 式の中だけで、抜けるとグローバルな $a は埋戻されます。 ==== reduce ==== 組込み関数に reduce がなかったので実装しました。 ;[https://paiza.io/projects/Oow9-sgUyfHJ2HcPcyoH2w?language=perl 例]:<syntaxhighlight lang=perl line copy> use v5.30.0; use warnings; sub reduce(&@) { my ( $cbr, @ary ) = @_; my $init = shift @ary; $init = $cbr->( local $a = $init, local $b = $_ ) foreach @ary; return $init; } say reduce { $_[0] + $_[1] } 1 .. 10; say reduce { $_[0] . $_[1] } "A" .. "Z"; say reduce { $a + $b } 1 .. 10; say reduce { $a . $b } "A" .. "Z"; </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> 55 ABCDEFGHIJKLMNOPQRSTUVWXYZ 55 ABCDEFGHIJKLMNOPQRSTUVWXYZ </syntaxhighlight> : 組込み関数 sort の様に、<var>$a</var> と <var>$b</var> でコードブロックに引数を渡すこともできるようにしました。 ==== filter ==== 組込み関数に grep は、多くの言語で filter の名前で知られる関数です。 ;[https://paiza.io/projects/Zm4tMKt5pp0--A61grEvGg?language=perl 例]:<syntaxhighlight lang=perl line copy> use v5.30.0; sub filter(&@) { my ( $cbr, @ary ) = @_; my @result = (); $cbr->( local $a = $_ ) ? push( @result, $_ ) : 0 foreach @ary; return @result; } say filter { $_ % 2 == 1; } 1 .. 10; say filter { $a % 2 == 1; } 1 .. 10; say grep { $_ % 2 == 1; } 1 .. 10; say filter { index( "Hello world", $_ ) >= 0 } ( "A" .. "Z", "a" .. "z" ); say filter { index( "Hello world", $a ) >= 0 } ( "A" .. "Z", "a" .. "z" ); say grep { index( "Hello world", $_ ) >= 0 } ( "A" .. "Z", "a" .. "z" ); say "@{[ map { $_ * 2 } filter { $_ % 2 == 1; } 1..10]}"; say "@{[ map { $_ * 2 } filter { $a % 2 == 1; } 1..10]}"; say "@{[ map { $_ * 2 } grep { $_ % 2 == 1; } 1..10]}"; </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> 13579 13579 13579 Hdelorw Hdelorw Hdelorw 2 6 10 14 18 2 6 10 14 18 2 6 10 14 18 </syntaxhighlight> : 組込み関数 sort の様に、<var>$a</var> でコードブロックに引数を渡すこともできるようにしました。 === コードリファレンス === ==== コードリファレンスとは? ==== Perlにおいて「コードリファレンス」とは、'''サブルーチンへの参照(ポインタ)'''を指します。通常、サブルーチンはその名前を使って呼び出しますが、名前ではなくリファレンス(参照)として扱うことで、動的にサブルーチンを渡したり、配列やハッシュに格納して柔軟に操作することができます。 ==== コードリファレンスの生成 ==== サブルーチンのリファレンスを生成するには、<code>\&</code>を使います。以下はその例です。 :<syntaxhighlight lang=perl copy> sub greet { return "Hello, World!\n"; } my $greet_ref = \&greet; # サブルーチンのリファレンスを取得 </syntaxhighlight> ここで、<code>$greet_ref</code>は<code>greet</code>というサブルーチンへの参照を保持するスカラ変数になります。 ==== コードリファレンスの呼び出し ==== コードリファレンスを呼び出すには、<code>-></code>演算子を使用します。または、<code>&</code>を使うことでも呼び出せます。 :<syntaxhighlight lang=perl copy> # 例: コードリファレンスを使用してサブルーチンを呼び出す print $greet_ref->(); # Hello, World! # & を使っても同様 print &$greet_ref(); # Hello, World! </syntaxhighlight> ==== コードリファレンスを使う利点 ==== # '''柔軟性''': サブルーチンを動的に渡せるため、特定の条件に応じて異なるサブルーチンを実行するようなコードが書けます。 # '''データ構造との組み合わせ''': 配列やハッシュにサブルーチンのリファレンスを格納することで、複数のサブルーチンを一括管理できます。 :<syntaxhighlight lang=perl copy> sub greet { return "Hello, World!\n"; } sub farewell { return "Goodbye, World!\n"; } # ハッシュでサブルーチンリファレンスを管理 my %actions = ( greet => \&greet, farewell => \&farewell, ); # 動的にサブルーチンを選んで実行 print $actions{greet}->(); # Hello, World! print $actions{farewell}->(); # Goodbye, World! </syntaxhighlight> ==== 無名サブルーチン ==== コードリファレンスは、無名サブルーチン(名前を持たないサブルーチン)を生成する際にも使用されます。無名サブルーチンは<code>sub</code>キーワードだけで定義され、直接リファレンスとして利用できます。 :<syntaxhighlight lang=perl copy> my $say_hello = sub { return "Hello from anonymous sub!\n"; }; # コードリファレンスとして呼び出す print $say_hello->(); # Hello from anonymous sub! </syntaxhighlight> ==== コードリファレンスの利用例 ==== ===== サブルーチンの引数として渡す ===== コードリファレンスを使うことで、サブルーチンを引数として別のサブルーチンに渡すことができます。 :<syntaxhighlight lang=perl copy> sub execute_function { my ($func_ref) = @_; print $func_ref->("Executing function!\n"); } # 無名サブルーチンを渡す execute_function(sub { print @_ }); </syntaxhighlight> ===== イベント処理 ===== イベント駆動型プログラミングやコールバック処理で、特定のタイミングで実行するサブルーチンを指定する場合に役立ちます。 :<syntaxhighlight lang=perl copy> sub on_event { my ($callback) = @_; print "Event occurred!\n"; $callback->(); } on_event(sub { print "Callback executed!\n" }); </syntaxhighlight> ===== ディスパッチテーブル ===== 関数の実行を名前で動的に選択する「ディスパッチテーブル」を構築する際に利用されます。 :<syntaxhighlight lang=perl copy> sub add { return $_[0] + $_[1]; } sub subtract { return $_[0] - $_[1]; } my %dispatch = ( add => \&add, subtract => \&subtract, ); my $operation = 'add'; print $dispatch{$operation}->(10, 5); # 15 </syntaxhighlight> ==== まとめ ==== コードリファレンスは、サブルーチンをリファレンスとして動的に扱える仕組みです。これにより、柔軟で再利用性の高いコードを記述できるようになります。特に、動的な処理やコールバックを必要とする場面では非常に有用です。また、無名サブルーチンを利用することで、簡潔で用途に応じたコードが書けるようになります。 ---- [TODO:スコープルールに関する簡素な説明] [TODO:コンテキストに関する例をふんだんに使った解説] == 永続的スコープのレキシカル変数 == my で宣言した変数(レキシカル変数)はサブルーチンを抜けると御破算になりますが、state で宣言した変数はレキシカルスコープであるものの次にサブルーチンが呼ばれたときも値を憶えています。 ;[https://paiza.io/projects/BUEgIyXqdobwwm9p3VsmKw?language=perl state $var]:<syntaxhighlight lang=perl line highlight="6" copy> #!/usr/bin/perl use v5.10; sub func { my $myVar = 0; state $stateVar = 0; $myVar++; $stateVar++; CORE::say "\$myVar = $myVar, \$stateVar = $stateVar"; } &func for 1..5 </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> $myVar = 1, $stateVar = 1 $myVar = 1, $stateVar = 2 $myVar = 1, $stateVar = 3 $myVar = 1, $stateVar = 4 $myVar = 1, $stateVar = 5 </syntaxhighlight> : state $var は、Perl 5.10 以降でサポートされています。 == コンテキストとwantarray関数 == 同じサブルーチンを呼出しても、スカラーが戻ることを期待している文脈(スカラーコンテキスト)と、リストが戻ることを期待している文脈(リストコンテキスト)の2通りがあります。 この2つのケースを判別するために wantarray 関数が用意されています。 ;[https://paiza.io/projects/wjGlO4nNueGJHx_A17Zh-g?language=perl コンテキストとwantarray関数]:<syntaxhighlight lang=perl line highlight=6 copy> #!/usr/bin/perl use strict; use warnings; sub func { return map { $_ * 2 } @_ if wantarray(); my $sum = 0; $sum += $_ for @_; return $sum; } my @x = func(0, 1, 2); my $y = func(0, 1, 2); print <<EOS; my \@x = func(0, 1, 2); \@x -> @x my \$y = func(0, 1, 2); \$y -> $y @{[ func(1,2,3) ]} @{[ scalar func(1,2,3)]} @{[ ~~ func(1,2,3)]} EOS </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> my @x = func(0, 1, 2); @x -> 0 2 4 my $y = func(0, 1, 2); $y -> 3 2 4 6 6 6 </syntaxhighlight> :wantarray 関数は、サブルーチンを呼出したコンテキストで戻値としてリストが要求されているなら真を、スカラーが要求されているなら偽を返します。 :関数 func は、リストコンテキストでは全ての要素を二倍にしたリストを、スカラーコンテキストでは全ての要素の合計を返します。 == 組込み関数の一覧 == {{Main|[https://perldoc.perl.org/5.36.0/perlfunc perlfunc(en)]|[https://perldoc.jp/docs/perl/5.36.0/perlfunc.pod perlfunc(ja)]}} === 文字列:String === [[#chomp|chomp]] [[#chop|chop]] [[#chr|chr]] [[#crypt|crypt]] [[#fc|fc]] [[#hex|hex]] [[#index|index]] [[#lc|lc]] [[#lcfirst|lcfirst]] [[#length|length]] [[#oct|oct]] [[#ord|ord]] [[#pack|pack]] [[#q/STRING/|q/STRING/]] [[#qq/STRING/|qq/STRING/]] [[#reverse|reverse]] [[#rindex|rindex]] [[#sprintf|sprintf]] [[#substr|substr]] [[#tr///|tr///]] [[#uc|uc]] [[#ucfirst|ucfirst]] [[#y///|y///]] ==== index ==== ;書式:<syntaxhighlight lang=perl>index STR, SUBSTR [, POSITION]</syntaxhighlight copy> ;機能:<syntaxhighlight lang=text>文字列STRの中で、部分文字列SUBSTRが最初に出現する位置を返します。</syntaxhighlight> ;[https://paiza.io/projects/PYUKqfDhk7sSi9l1Sdwsxg?language=perl 組込み関数 index]:<syntaxhighlight lang=perl copy> #!/usr/bin/env perl use v5.30.0; use warnings; my $str = "This is a pen."; my $substr = "pen"; say qq(index "$str", "$substr" -> @{[index $str, $substr]}); $str = "これは、ペンです。"; $substr = "ペン"; say qq(index "$str", "$substr" -> @{[index $str, $substr]}); use Encode qw(encode decode); $str = decode('utf-8', "これは、ペンです。"); $substr = decode('utf-8', "ペン"); say encode('utf-8', qq(index "$str", "$substr" -> @{[index $str, $substr]})); </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> index "This is a pen.", "pen" -> 10 index "これは、ペンです。", "ペン" -> 12 index "これは、ペンです。", "ペン" -> 4 </syntaxhighlight> :<code>pen</code> の前にある <code>This is a </code> は空白も含めて合計で10文字なので、<code>10</code> が表示されます。 ::{| class=wikitable |+ 文字列とインデックスの対応 !0!!1!!2!!3!!4!!5!!6!!7!!8!!9!!10!!11!!12!!13 |- style="text-align: center" |T||h||i||s|| ||i||s|| ||a|| ||p||e||n||. |} ;解説 :indexは、文字列 STR の中から、検索文字列 SUBSTR を探し、'''最初'''に見つかった位置を返します。検索文字が見つからない場合には、-1 が返ります。 :省略可能な引数、POSITION には、検索開始位置を指定します(ディフォルトは0)。 ::POSITION を使うと部分文字列が2回め以降に出現する位置も確かめることが出来、部分文字列の長さに注意すれば部分文字列の出現回数を数えることなどが容易になります。 :位置が 0 から始まることに留意しましょう。 0 は文字列の左端を表します。 :位置は文字単位ではなくバイト数なので、ソースコードエンコーディングが UTF-8 で多バイト文字が交じると、文字数とバイト数に食い違いが生じます。 :このような場合は Encode モジュールを使い内部形式( ''internal format'' )に変換します。 :内部形式であれば、サロゲートペアにも対応できますが、合成文字は修飾コードと基底文字はそれぞれ1文字に数えられます。 ===== utf8プラグマを使う ===== ;[https://paiza.io/projects/cp_epUyDzcstlfbqp6Z82A?language=perl 別解(utf8プラグマを使う)]:<syntaxhighlight lang=perl copy> #!/usr/bin/perl use v5.30.0; use warnings; use utf8; my $str = "This is a pen."; my $substr = "pen"; say qq(index "$str", "$substr" -> @{[index $str, $substr]}); use Encode qw(encode); $str = "これは、ペンです。"; $substr = "ペン"; say encode('utf-8', qq(index "$str", "$substr" -> @{[index $str, $substr]})); </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> index "This is a pen.", "pen" -> 10 index "これは、ペンです。", "ペン" -> 4 </syntaxhighlight> : utf8プラグマを使い、ソースコードエンコーディングが UTF-8 であることを明示すると、文字リテラルは内部形式に変換され index や length で処理されます。 : この場合でも、出力するときに内部形式から UTF-8 にエンコードする必要があります。 ===== binmodeを使う ===== ;[https://paiza.io/projects/cp_epUyDzcstlfbqp6Z82A?language=perl 別解(binmodeを使う)]:<syntaxhighlight lang=perl highlight=5 copy> #!/usr/bin/perl use v5.30.0; use warnings; use utf8; binmode STDOUT,":encoding(UTF-8)"; my $str = "This is a pen."; my $substr = "pen"; say qq(index "$str", "$substr" -> @{[index $str, $substr]}); $str = "これは、ペンです。"; $substr = "ペン"; say qq(index "$str", "$substr" -> @{[index $str, $substr]}); </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> index "This is a pen.", "pen" -> 10 index "これは、ペンです。", "ペン" -> 4 </syntaxhighlight> : 毎回エンコードせず、STDOUT のディフォルトエンコーディングを UTF-8 にかえました。 : <code>binmode STDIN,":encoding(UTF-8)";</code>と<code>binmode STDERR,":encoding(UTF-8)";</code>も同時に指定したほうがいいかもしれません。 テキストのエンコーディングは、Perlを使っていると度々トラブルのもとになるので、回避方法が幾つかある事を知っておくと、他人の書いたコードを読むときなどに役に立ちます。 ここで紹介した方法の他に、歌代さんのjcode.plなどもあるのですが、標準モジュールの範囲の説明に留めました。 ==== rindex ==== ;書式:<syntaxhighlight lang=perl>rindex (STR, SUBSTR, [POSITION])</syntaxhighlight copy> ;機能:文字列STRの中で、部分文字列SUBSTRが'''最後'''に出現する位置を返します。 ;[https://paiza.io/projects/BcE2R3K_lHUDlhNTQWwY3Q?language=perl 組込み関数 rindex]:<syntaxhighlight lang=perl copy> #!/usr/bin/perl use v5.30.0; use warnings; use utf8; binmode STDOUT,":encoding(UTF-8)"; my $str = "I like pens and pencils."; my $substr = "pen"; say qq(rindex "$str", "$substr" -> @{[rindex $str, $substr]}); $str = "私は筆と鉛筆が好きです。"; $substr = "筆"; say qq(rindex "$str", "$substr" -> @{[rindex $str, $substr]}); </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> rindex "I like pens and pencils.", "pen" -> 16 rindex "私は筆と鉛筆が好きです。", "筆" -> 5 </syntaxhighlight> ;解説 :rindexは、文字列 STR の中から、検索文字列 SUBSTR を探し、'''最後'''に見つかった位置を返します(「末尾からの位置を返す」との編集が過去にありましたが、間違いです)。検索文字が見つからない場合には、-1 が返ります。 :省略可能な引数、POSITION には、検索開始位置を指定します(ディフォルトは0)。 {{See also|#index}} ==== substr ==== ;書式:<syntaxhighlight lang=perl>substr (EXPR, OFFSET, [LENGTH], [REPLACEMENT])</syntaxhighlight copy> 文字列 EXPR から、OFFSET 目以降のバイト列を返します。取り出す長さ LENGTH をバイト単位で指定できますが、省略した場合は文字列の最後まで取り出します。なお、utf8プラグマが有効な場合は、バイト単位ではなく文字単位で取り出すことができます。 位置情報 OFFSET は上述のとおり 0 から始まりますが、LENGTH は容量なので通常は 1 以上の値を指定します。 文字列 REPLACEMENT を指定すると、取り出される部分を REPLACEMENT で置換します。 ;[https://paiza.io/projects/BcE2R3K_lHUDlhNTQWwY3Q?language=perl 組込み関数 rindex]:<syntaxhighlight lang=perl copy> #!/usr/bin/perl use v5.30.0; use warnings; use utf8; binmode STDOUT,":encoding(UTF-8)"; my $str = "Hello, world!"; say substr($str, index($str, "world"), length("world"), "Japan"); say $str; $str = "こんにちは、世界!"; say substr($str, index($str, "世界"), length("世界"), "🌍"); say $str; </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> world Hello, Japan! 世界 こんにちは、🌍! </syntaxhighlight> ==== uc ==== ;書式:<syntaxhighlight lang=perl>uc ([EXPR])</syntaxhighlight copy> :文字列 EXPR を大文字にして返します。EXPR を省略すると、$_ が使われます。 ==== ucfirst ==== ;書式:<syntaxhighlight lang=perl>ucfirst ([EXPR])</syntaxhighlight copy> :uc と同じですが、先頭1文字を大文字にして返します。 ==== lc ==== ;書式:<syntaxhighlight lang=perl>lc ([EXPR])</syntaxhighlight copy> :uc と同じですが、小文字にして返します。 ==== lcfirst ==== ;書式:<syntaxhighlight lang=perl>lcfirst ([EXPR])</syntaxhighlight copy> :ucfirst と同じですが、小文字にして返します。 ==== chop ==== ;書式:<syntaxhighlight lang=perl copy> chop VARIABLE chop (LIST) </syntaxhighlight> : 変数 VARIABLE の末尾の末尾1文字を削除します。 : 変数のリストを渡された場合は、各変数について同じ処理を行います。 : VARIABLE を省略すると $_ が使われます。 ;[https://paiza.io/projects/Fva3C8fhvsNLFSY7C4WNTw?language=perl chopとchomp]:<syntaxhighlight lang=perl copy> #!/usr/bin/perl use v5.30.0; use warnings; use utf8; binmode STDOUT,":encoding(UTF-8)"; my $str = "Hello, world!\n"; chop $str; say "chop: $str(@{[length $str]})"; chop $str; say "chop: $str(@{[length $str]})"; chop $str; say "chop: $str(@{[length $str]})"; chop $str; say "chop: $str(@{[length $str]})"; $str = "Hello, world!\n"; chomp $str; say "chomp: $str(@{[length $str]})"; chomp $str; say "chomp: $str(@{[length $str]})"; chomp $str; say "chomp: $str(@{[length $str]})"; chomp $str; say "chomp: $str(@{[length $str]})"; $str = "Hello, world!\n"; $str = substr($str, 0, length($str) - 1); say "substr(): $str(@{[length $str]})"; $str = substr($str, 0, length($str) - 1); say "substr(): $str(@{[length $str]})"; $str = substr($str, 0, length($str) - 1); say "substr(): $str(@{[length $str]})"; sub chop(\$) { my $strr = shift; $$strr = substr($$strr, 0, length($$strr) - 1); undef } $str = "Hello, world!\n"; ::chop $str; say "::chop: $str(@{[length $str]})"; ::chop $str; say "::chop: $str(@{[length $str]})"; ::chop $str; say "::chop: $str(@{[length $str]})"; sub chomp(\$) { my $strr = shift; $$strr = substr($$strr, 0, length($$strr) - 1) if substr($$strr, length($$strr) - 1, 1) eq "\n"; undef } $str = "Hello, world!\n"; ::chomp $str; say "::chomp: $str(@{[length $str]})"; ::chomp $str; say "::chomp: $str(@{[length $str]})"; </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> chop: Hello, world!(13) chop: Hello, world(12) chop: Hello, worl(11) chop: Hello, wor(10) chomp: Hello, world!(13) chomp: Hello, world!(13) chomp: Hello, world!(13) chomp: Hello, world!(13) substr(): Hello, world!(13) substr(): Hello, world(12) substr(): Hello, worl(11) ::chop: Hello, world!(13) ::chop: Hello, world(12) ::chop: Hello, worl(11) ::chomp: Hello, world!(13) ::chomp: Hello, world!(13) </syntaxhighlight> : chop は、文字列を末尾から喰います(破壊的) : chomp は、文字列の末尾の改行を喰います(破壊的) ==== chomp ==== ;書式:<syntaxhighlight lang=perl copy> chomp VARIABLE chomp (LIST) </syntaxhighlight> : 変数 VARIABLE の末尾の $/(デフォルトは "\n")を削除します。 : 変数のリストを渡された場合は、各変数について同じ処理を行います。 : VARIABLE を省略すると $_ が使われます。 ==== chr ==== ; 書式:<syntaxhighlight lang=perl>chr [NUMBER]</syntaxhighlight copy> : 文字セットで NUMBER 番目に割り当てられている文字を返します。 : NUMBER を省略すると $_ が使われます。 : 逆の操作を行うには ord を使います。 ==== crypt ==== ;書式:<syntaxhighlight lang=perl>crypt PLAINTEXT, SALT</syntaxhighlight copy> :C ライブラリの crypt(3) をエミュレートします。 ==== hex ==== ;書式:<syntaxhighlight lang=perl>hex [EXPR]</syntaxhighlight copy> : 十六進数 EXPR を十進数に変換して返します。EXPR を省略すると $_ が使われます。 ==== length ==== ;書式:<syntaxhighlight lang=perl>length [EXPR]</syntaxhighlight copy> : 文字列 EXPR の長さを返します。bytes プラグマが有効な場合(デフォルト)はバイト数を、utf8 プラグマが有効な場合は文字数を返します。EXPR を省略すると $_ が使われます。 ==== oct ==== ;書式:<syntaxhighlight lang=perl>oct [EXPR]</syntaxhighlight copy> : 八進数 EXPR を十進数に変換して返します。EXPR を省略すると $_ が使われます。 ==== ord ==== ;書式:<syntaxhighlight lang=perl>ord [EXPR]</syntaxhighlight copy> : 文字列 EXPR の文字セット上でのコード位置を返します。EXPR を省略すると $_ が使われます。逆の操作を行うには chr を使います。 ==== pack ==== <code>pack</code>関数は、指定されたフォーマットに従ってデータをバイナリ文字列にパックします。主にバイナリデータを処理する際に使用されます。以下は、<code>pack</code>関数の基本的な使用例と一般的なフォーマット指定子の一部です。 :<syntaxhighlight lang=perl copy> # バイナリ文字列を作成する例 my $binary_data = pack('C4 A4', 65, 66, 67, 68, 'Hello'); # 上記の例では、'C4'は4つのバイトの符号なし整数を、'A4'は4バイトの文字列を表します。 # したがって、65, 66, 67, 68はバイナリデータにパックされ、それに続く'Hello'もパックされます。 # バイナリ文字列を出力する print "$binary_data\n"; # 出力例: ABCDHello # バイナリ文字列からデータをアンパックする例 my ($num1, $num2, $string) = unpack('C4 A4', $binary_data); print "$num1 $num2 $string\n"; # 出力例: 65 66 Hello </syntaxhighlight> 一般的なフォーマット指定子の一部は以下の通りです。 * <code>A</code> - 文字列 * <code>C</code> - 符号なし8ビット整数 (1 バイト) * <code>S</code> - 符号なし16ビット整数 (2 バイト) * <code>L</code> - 符号なし32ビット整数 (4 バイト) * <code>n</code> - ネットワークバイトオーダー (big-endian) の16ビット整数 * <code>N</code> - ネットワークバイトオーダーの32ビット整数 * <code>v</code> - リトルエンディアンバイトオーダーの16ビット整数 * <code>V</code> - リトルエンディアンバイトオーダーの32ビット整数 <code>pack</code>関数と<code>[[#unpack|unpack]]</code>関数は、バイナリデータを他の形式に変換する際に非常に便利です。 ==== q ==== <pre> q/STRING/ qq/STRING/ qr/STRING/ qx/STRING/ qw/STRING/ </pre> シングルクォート、ダブルクォート、正規表現、バッククォート、単語クォート。詳細は[[Perl/演算子|演算子]]の章を参照。 ==== reverse ==== ;書式:<syntaxhighlight lang=perl>reverse LIST</syntaxhighlight copy> :リストコンテキストでは LIST の順番を逆順にしたリストを返します。スカラーコンテキストでは LIST の要素を結合した後に逆順にした文字列を返します。 ;[https://paiza.io/projects/m274vBmTsDDTfyB1yKQy3g?language=perl 例]:<syntaxhighlight lang=perl copy> use v5.30.0; use warnings; my @array = qw(あい うえお かきくけこ 🏝); say "@{[ reverse @array ]}"; say "@{[ scalar reverse @array ]}"; use utf8; binmode STDOUT,":encoding(UTF-8)"; @array = qw(あい うえお かきくけこ 🏝); say "@{[ reverse @array ]}"; say "@{[ scalar reverse @array ]}"; </syntaxhighlight> ;実行結果:<syntaxhighlight lang=perl copy> 🏝 かきくけこ うえお あい ����㑁㏁㍁㋁㊁㈁ㆁめ� 🏝 かきくけこ うえお あい 🏝こけくきかおえういあ </syntaxhighlight> : Perlの文字列はディフォルトではバイトシーケンスなのでバイト逆順にすると多バイト文字は破綻し、上記のように文字化けします。 : use utf8;で、バイトシーケンスから内部エンコーディング( ''Wide character'' )に切替えることができますが、このまま say すると内部エンコーディングのままなので、標準出力のレイヤーを ":encoding(UTF-8)" に変更します。 ==== sprintf ==== ;書式:<syntaxhighlight lang=perl>sprintf FORMAT, LIST</syntaxhighlight copy> :LIST を FORMAT に従って整形して返します。 ==== tr ==== ;書式:<syntaxhighlight lang=perl>tr///</syntaxhighlight copy> :1文字を対応する1文字に置換します。詳細は[[Perl/演算子|演算子]]の章を参照。 ==== y ==== ;書式:<syntaxhighlight lang=perl>y///</syntaxhighlight copy> :tr///と同義。 === 正規表現とパターンマッチ === Perlには、テキスト処理やパターンマッチングに役立つ様々な機能が備わっています。 以下では、それぞれの機能について解説します。 ; m// 演算子 : パターンマッチング演算子として知られており、文字列内でパターンを検索するために使用されます。正規表現を使ってパターンを指定し、マッチした部分を取得したり、マッチングの成否を確認したりすることができます。例えば、次のように使用します。 :<syntaxhighlight lang=perl copy> my $str = "Hello World"; if ($str =~ m/World/) { print "マッチしました\n"; } </syntaxhighlight> ; pos 関数 : <code>pos</code> 関数は、直近のパターンマッチングでマッチした部分文字列の次の検索位置を示します。次のパターンマッチングでの開始位置を変更するために使用されます。例えば、次のように使用します。 :<syntaxhighlight lang=perl copy> my $str = "abcabc"; while ($str =~ m/abc/g) { print "マッチ位置: " . pos($str) . "\n"; } </syntaxhighlight> ; qr// 演算子 : <code>qr//</code> 演算子は、正規表現を表すオブジェクトを生成します。これは、後で同じ正規表現を再利用する場合に便利です。例えば、次のように使用します。 :<syntaxhighlight lang=perl copy> my $pattern = qr/abc/; if ($str =~ $pattern) { print "マッチしました\n"; } </syntaxhighlight> ; quotemeta 関数 : <code>quotemeta</code> 関数は、文字列内のメタ文字をエスケープします。これにより、文字列をそのままパターンとして使用する際に、意図しない動作を防ぐことができます。例えば、次のように使用します。 :<syntaxhighlight lang=perl copy> my $str = "a+"; my $pattern = quotemeta($str); if ($input =~ m/$pattern/) { print "マッチしました\n"; } </syntaxhighlight> ; s/// 演算子 : 置換演算子として知られており、文字列内のパターンにマッチする部分を置換します。例えば、次のように使用します。 :<syntaxhighlight lang=perl copy> my $str = "apple orange"; $str =~ s/apple/banana/; print $str; # "banana orange" を出力 </syntaxhighlight> ; split 関数 : <code>split</code> 関数は、文字列を指定した区切り文字で分割し、リストに格納します。例えば、次のように使用します。 :<syntaxhighlight lang=perl copy> my $str = "apple,orange,banana"; my @fruits = split /,/, $str; print join("-", @fruits); # "apple-orange-banana" を出力 </syntaxhighlight> ; study 関数 : <code>study</code> 関数は、文字列内のパターンマッチングを高速化するための最適化を行います。特に大きな文字列を検索する場合に有効です。例えば、次のように使用します。 :<syntaxhighlight lang=perl copy> my $str = "very long string..."; study $str; if ($str =~ m/pattern/) { print "マッチしました\n"; } </syntaxhighlight> これらの機能を使用することで、Perlでのテキスト処理やパターンマッチングを効率的に行うことができます。 === 数値演算関数 === ; abs, atan2, cos, exp, hex, int, log, oct, rand, sin, sqrt, srand これらの関数はPerlで数学的な操作を行うための組み込み関数です。以下にそれぞれの関数を簡単に説明します。 ; abs : 数値の絶対値を返します。 :<syntaxhighlight lang=perl copy> my $num = -10; my $abs_num = abs($num); # $abs_num には 10 が格納されます </syntaxhighlight> ; atan2 : y/x のアークタンジェントを返します。y と x を引数に取ります。 :<syntaxhighlight lang=perl copy> my $atan_value = atan2($y, $x); </syntaxhighlight> ; cos : 弧度法で与えられた角度の余弦を返します。 :<syntaxhighlight lang=perl copy> my $cos_value = cos($angle); </syntaxhighlight> ; exp : 指数関数 <math>e^x</math> を返します。 :<syntaxhighlight lang=perl copy> my $exp_value = exp($x); </syntaxhighlight> ; hex : 文字列を 16 進数として解釈し、整数値を返します。 :<syntaxhighlight lang=perl copy> my $hex_value = hex("0xFF"); # $hex_value には 255 が格納されます </syntaxhighlight> ; int : 数値の整数部分を返します。 :<syntaxhighlight lang=perl copy> my $num = 3.6; my $int_num = int($num); # $int_num には 3 が格納されます </syntaxhighlight> ; log : 自然対数を返します。 :<syntaxhighlight lang=perl copy> my $log_value = log($x); </syntaxhighlight> ; oct : 文字列を 8 進数として解釈し、整数値を返します。 :<syntaxhighlight lang=perl copy> my $oct_value = oct("077"); # $oct_value には 63 が格納されます </syntaxhighlight> ; rand : 0 から 1 未満の乱数を返します。 :<syntaxhighlight lang=perl copy> my $rand_value = rand(); </syntaxhighlight> ; sin : 弧度法で与えられた角度の正弦を返します。 :<syntaxhighlight lang=perl copy> my $sin_value = sin($angle); </syntaxhighlight> ; sqrt : 数値の平方根を返します。 :<syntaxhighlight lang=perl copy> my $sqrt_value = sqrt($num); </syntaxhighlight> ; srand : 乱数のシードを設定します。これにより、乱数生成のシーケンスが初期化されます。 :<syntaxhighlight lang=perl copy> srand(time); # 現在時刻をシードとして使用する例 </syntaxhighlight> これらの関数を使用することで、Perlでの数学的な操作を簡単に行うことができます。 === 配列操作 === ; each, keys, pop, push, shift, splice, unshift, values これらの関数は、Perlで配列を操作するための組み込み関数です。以下にそれぞれの関数を説明します。 ; each : 配列を反復処理するために使用されます。各反復で、配列内の次の要素を返します。 :<syntaxhighlight lang=perl copy> my @array = ('a', 'b', 'c'); while (my ($index, $value) = each @array) { print "Index: $index, Value: $value\n"; } </syntaxhighlight> ; keys : 配列のインデックス(キー)を取得します。通常、数値のインデックスが返されます。 :<syntaxhighlight lang=perl copy> my @array = ('a', 'b', 'c'); my @indices = keys @array; # @indices には (0, 1, 2) が格納されます </syntaxhighlight> ; pop : 配列の末尾から要素を取り出します(削除)。 :<syntaxhighlight lang=perl copy> my @array = (1, 2, 3, 4, 5); my $last_element = pop @array; # $last_element には 5 が格納され、@array は (1, 2, 3, 4) になります </syntaxhighlight> ; push : 配列の末尾に要素を追加します。 :<syntaxhighlight lang=perl copy> my @array = (1, 2, 3); push @array, 4, 5; # @array は (1, 2, 3, 4, 5) になります </syntaxhighlight> ; shift : 配列の先頭から要素を取り出します(削除)。 :<syntaxhighlight lang=perl copy> my @array = (1, 2, 3, 4, 5); my $first_element = shift @array; # $first_element には 1 が格納され、@array は (2, 3, 4, 5) になります </syntaxhighlight> ; splice : 配列の指定した範囲の要素を置換または削除し、新しい要素を挿入します。 :<syntaxhighlight lang=perl copy> my @array = (1, 2, 3, 4, 5); splice @array, 2, 2, 'a', 'b'; # @array は (1, 2, 'a', 'b', 5) になります </syntaxhighlight> ; unshift : 配列の先頭に要素を追加します。 :<syntaxhighlight lang=perl copy> my @array = (3, 4, 5); unshift @array, 1, 2; # @array は (1, 2, 3, 4, 5) になります </syntaxhighlight> これらの関数を使用することで、Perlで配列を効果的に操作することができます。 === リスト操作 === ; grep, join, map, qw//, reverse, sort, unpack Perlにおけるリスト操作に関する関数や演算子について解説します。 ; grep : リスト内の要素をフィルタリングします。条件にマッチする要素だけを残した新しいリストを返します。 :<syntaxhighlight lang=perl copy> my @numbers = (1, 2, 3, 4, 5); my @even_numbers = grep { $_ % 2 == 0 } @numbers; # @even_numbers には (2, 4) が格納されます </syntaxhighlight> ; join : リストの要素を指定した区切り文字で結合して文字列を生成します。 :<syntaxhighlight lang=perl copy> my @words = ('hello', 'world', '!'); my $sentence = join ' ', @words; # $sentence には "hello world !" が格納されます </syntaxhighlight> ; map : リストの各要素に対して処理を行い、結果を新しいリストとして返します。 :<syntaxhighlight lang=perl copy> my @numbers = (1, 2, 3); my @squared_numbers = map { $_ ** 2 } @numbers; # @squared_numbers には (1, 4, 9) が格納されます </syntaxhighlight> ; qw// : クォートワード演算子。文字列をスペースで区切り、リストとして返します。 :<syntaxhighlight lang=perl copy> my @words = qw(apple banana orange); # @words には ('apple', 'banana', 'orange') が格納されます </syntaxhighlight> ; reverse : リストの要素の順序を逆にします。 :<syntaxhighlight lang=perl copy> my @numbers = (1, 2, 3); my @reversed_numbers = reverse @numbers; # @reversed_numbers には (3, 2, 1) が格納されます </syntaxhighlight> ; sort : リストの要素をソートします。デフォルトでは文字列としてソートされますが、数値としてソートしたい場合は <code>sort {$a <=> $b}</code> のようにコードブロックを指定します。 :<syntaxhighlight lang=perl copy> my @numbers = (3, 1, 2); my @sorted_numbers = sort @numbers; # @sorted_numbers には (1, 2, 3) が格納されます </syntaxhighlight> ; unpack : 文字列を指定したテンプレートに従って解釈し、リストとして返します。主にバイナリデータの処理に使用されます。 :<syntaxhighlight lang=perl copy> my $binary_data = "\x41\x42\x43"; my @values = unpack 'C*', $binary_data; # @values には (65, 66, 67) が格納されます </syntaxhighlight> これらの関数や演算子を使うことで、Perlでリストを効果的に操作することができます。 === ハッシュ操作 === ; delete, each, exists, keys, values Perlにおけるハッシュ(連想配列)の操作について説明します。 ; delete : ハッシュから指定したキーとその関連する値を削除します。 :<syntaxhighlight lang=perl copy> my %hash = ('a' => 1, 'b' => 2, 'c' => 3); delete $hash{'b'}; # ハッシュから 'b' キーとその値を削除します </syntaxhighlight> ; each : ハッシュを反復処理するために使用されます。各反復で、キーと値のペアを返します。 :<syntaxhighlight lang=perl copy> my %hash = ('a' => 1, 'b' => 2, 'c' => 3); while (my ($key, $value) = each %hash) { print "$key: $value\n"; } </syntaxhighlight> ; exists : 指定したキーがハッシュ内に存在するかどうかを確認します。存在する場合は真を返し、存在しない場合は偽を返します。 :<syntaxhighlight lang=perl copy> my %hash = ('a' => 1, 'b' => 2, 'c' => 3); if (exists $hash{'b'}) { print "'b' キーは存在します\n"; } else { print "'b' キーは存在しません\n"; } </syntaxhighlight> ; keys : ハッシュのキーのリストを取得します。 :<syntaxhighlight lang=perl copy> my %hash = ('a' => 1, 'b' => 2, 'c' => 3); my @keys = keys %hash; # @keys には ('a', 'b', 'c') が格納されます </syntaxhighlight> ; values : ハッシュの値のリストを取得します。 :<syntaxhighlight lang=perl copy> my %hash = ('a' => 1, 'b' => 2, 'c' => 3); my @values = values %hash; # @values には (1, 2, 3) が格納されます </syntaxhighlight> これらの関数や演算子を使うことで、Perlでハッシュを効果的に操作することができます。 === I/O === ; binmode, close, closedir, dbmclose, dbmopen, die, eof, fileno, flock, format, getc, print, printf, read, readdir, readline, rewinddir, say, seek, seekdir, select, syscall, sysread, sysseek, syswrite, tell, telldir, truncate, warn, write === 固定長データとレコード === ; pack, read, syscall, sysread, sysseek, syswrite, unpack, vec === ファイルハンドル・ファイルとディレクトリ === ; -X, chdir, chmod, chown, chroot, fcntl, glob, ioctl, link, lstat, mkdir, open, opendir, readlink, rename, rmdir, select, stat, symlink, sysopen, umask, unlink, utime === 制御構造 === ; break, caller, continue, die, do, dump, eval, evalbytes, exit, __FILE__, goto, last, __LINE__, next, __PACKAGE__, redo, return, sub, __SUB__, wantarray === スコープ === ; caller, import, local, my, our, package, state, use === Misc. === ; defined, formline, lock, prototype, reset, scalar, undef === プロセス === ; alarm, exec, fork, getpgrp, getppid, getpriority, kill, pipe, qx//, readpipe, setpgrp, setpriority, sleep, system, times, wait, waitpid === モジュール === ; do, import, no, package, require, use === オブジェクト指向 === ; bless, dbmclose, dbmopen, package, ref, tie, tied, untie, use === Socket === ; accept, bind, connect, getpeername, getsockname, getsockopt, listen, recv, send, setsockopt, shutdown, socket, socketpair === System V IPC === ; msgctl, msgget, msgrcv, msgsnd, semctl, semget, semop, shmctl, shmget, shmread, shmwrite === ユーザーとグループ === ; endgrent, endhostent, endnetent, endpwent, getgrent, getgrgid, getgrnam, getlogin, getpwent, getpwnam, getpwuid, setgrent, setpwent === ネットワーク情報 === ; endprotoent, endservent, gethostbyaddr, gethostbyname, gethostent, getnetbyaddr, getnetbyname, getnetent, getprotobyname, getprotobynumber, getprotoent, getservbyname, getservbyport, getservent, sethostent, setnetent, setprotoent, setservent === 日付時刻 === ; gmtime, localtime, time, times === 関数以外のキーワード === ; and, AUTOLOAD, BEGIN, catch, CHECK, cmp, CORE, __DATA__, default, defer, DESTROY, else, elseif, elsif, END, __END__, eq, finally, for, foreach, ge, given, gt, if, INIT, isa, le, lt, ne, not, or, try, UNITCHECK, unless, until, when, while, x, xor <!-- == 組込み関数の諸元表 == モジュール Pod::Functions を使うと組込み関数の諸元にアクセスできます。 ;[https://paiza.io/projects/laNg3WdoIiKX8kqnvfsXgQ?language=perl 組込み関数の一覧を得るコード]:<syntaxhighlight lang=perl copy> use strict; use warnings; use Pod::Functions; print join ' ', sort keys %Type </syntaxhighlight> ;実行結果:<syntaxhighlight lang=text> -X __FILE__ __LINE__ __PACKAGE__ __SUB__ abs accept alarm atan2 bind binmode bless break caller chdir chmod chomp chop chown chr chroot close closedir connect continue cos crypt dbmclose dbmopen defined delete die do dump each endgrent endhostent endnetent endprotoent endpwent endservent eof eval evalbytes exec exists exit exp fc fcntl fileno flock fork format formline getc getgrent getgrgid getgrnam gethostbyaddr gethostbyname gethostent getlogin getnetbyaddr getnetbyname getnetent getpeername getpgrp getppid getpriority getprotobyname getprotobynumber getprotoent getpwent getpwnam getpwuid getservbyname getservbyport getservent getsockname getsockopt glob gmtime goto grep hex import index int ioctl join keys kill last lc lcfirst length link listen local localtime lock log lstat m// map mkdir msgctl msgget msgrcv msgsnd my next no oct open opendir ord our pack package pipe pop pos print printf prototype push q/STRING/ qq/STRING/ qr/STRING/ quotemeta qw/STRING/ qx/STRING/ rand read readdir readline readlink readpipe recv redo ref rename require reset return reverse rewinddir rindex rmdir s/// say scalar seek seekdir select semctl semget semop send setgrent sethostent setnetent setpgrp setpriority setprotoent setpwent setservent setsockopt shift shmctl shmget shmread shmwrite shutdown sin sleep socket socketpair sort splice split sprintf sqrt srand stat state study sub substr symlink syscall sysopen sysread sysseek system syswrite tell telldir tie tied time times tr/// truncate uc ucfirst umask undef unlink unpack unshift untie use utime values vec wait waitpid wantarray warn write y/// </syntaxhighlight> ;[https://paiza.io/projects/ky9-H1JH3jc2wLnFpPMFVA?language=perl Wikitableを生成するコード]:<syntaxhighlight lang=perl copy> use v5.30.0; use Pod::Functions; # epilogue print <<EOS; :{| class="sortable wikitable" |+ 組込み関数の諸元 |- ! kind !! function !! flaver EOS # Kind -> Function -> Flavor my @kkeys = sort keys %Kinds; foreach my $kkey(@kkeys) { my ($kind, $functions) = %Kinds{$kkey}; foreach my $function(@$functions) { my ($null, $flaver) = %Flavor{$function}; print <<EOS |- | $kind || [[#$function|$function]] || $flaver EOS } } # prologue print <<EOS; |} EOS </syntaxhighlight> :{| class="sortable wikitable" |+ 組込み関数の諸元 |- ! kind !! function !! flaver |- | ARRAY || [[#each|each]] || retrieve the next key/value pair from a hash |- | ARRAY || [[#keys|keys]] || retrieve list of indices from a hash |- | ARRAY || [[#pop|pop]] || remove the last element from an array and return it |- | ARRAY || [[#push|push]] || append one or more elements to an array |- | ARRAY || [[#shift|shift]] || remove the first element of an array, and return it |- | ARRAY || [[#splice|splice]] || add or remove elements anywhere in an array |- | ARRAY || [[#unshift|unshift]] || prepend more elements to the beginning of a list |- | ARRAY || [[#values|values]] || return a list of the values in a hash |- | Binary || [[#pack|pack]] || convert a list into a binary representation |- | Binary || [[#read|read]] || fixed-length buffered input from a filehandle |- | Binary || [[#syscall|syscall]] || execute an arbitrary system call |- | Binary || [[#sysread|sysread]] || fixed-length unbuffered input from a filehandle |- | Binary || [[#sysseek|sysseek]] || position I/O pointer on handle used with sysread and syswrite |- | Binary || [[#syswrite|syswrite]] || fixed-length unbuffered output to a filehandle |- | Binary || [[#unpack|unpack]] || convert binary structure into normal perl variables |- | Binary || [[#vec|vec]] || test or set particular bits in a string |- | File || [[#-X|-X]] || a file test (-r, -x, etc) |- | File || [[#chdir|chdir]] || change your current working directory |- | File || [[#chmod|chmod]] || changes the permissions on a list of files |- | File || [[#chown|chown]] || change the ownership on a list of files |- | File || [[#chroot|chroot]] || make directory new root for path lookups |- | File || [[#fcntl|fcntl]] || file control system call |- | File || [[#glob|glob]] || expand filenames using wildcards |- | File || [[#ioctl|ioctl]] || system-dependent device control system call |- | File || [[#link|link]] || create a hard link in the filesystem |- | File || [[#lstat|lstat]] || stat a symbolic link |- | File || [[#mkdir|mkdir]] || create a directory |- | File || [[#open|open]] || open a file, pipe, or descriptor |- | File || [[#opendir|opendir]] || open a directory |- | File || [[#readlink|readlink]] || determine where a symbolic link is pointing |- | File || [[#rename|rename]] || change a filename |- | File || [[#rmdir|rmdir]] || remove a directory |- | File || [[#select|select]] || reset default output or do I/O multiplexing |- | File || [[#stat|stat]] || get a file's status information |- | File || [[#symlink|symlink]] || create a symbolic link to a file |- | File || [[#sysopen|sysopen]] || open a file, pipe, or descriptor |- | File || [[#umask|umask]] || set file creation mode mask |- | File || [[#unlink|unlink]] || remove one link to a file |- | File || [[#utime|utime]] || set a file's last access and modify times |- | Flow || [[#break|break]] || break out of a C<given> block |- | Flow || [[#caller|caller]] || get context of the current subroutine call |- | Flow || [[#continue|continue]] || optional trailing block in a while or foreach |- | Flow || [[#die|die]] || raise an exception or bail out |- | Flow || [[#do|do]] || turn a BLOCK into a TERM |- | Flow || [[#dump|dump]] || create an immediate core dump |- | Flow || [[#eval|eval]] || catch exceptions or compile and run code |- | Flow || [[#evalbytes|evalbytes]] || similar to string eval, but intend to parse a bytestream |- | Flow || [[#exit|exit]] || terminate this program |- | Flow || [[#__FILE__|__FILE__]] || the name of the current source file |- | Flow || [[#goto|goto]] || create spaghetti code |- | Flow || [[#last|last]] || exit a block prematurely |- | Flow || [[#__LINE__|__LINE__]] || the current source line number |- | Flow || [[#next|next]] || iterate a block prematurely |- | Flow || [[#__PACKAGE__|__PACKAGE__]] || the current package |- | Flow || [[#redo|redo]] || start this loop iteration over again |- | Flow || [[#return|return]] || get out of a function early |- | Flow || [[#sub|sub]] || declare a subroutine, possibly anonymously |- | Flow || [[#__SUB__|__SUB__]] || the current subroutine, or C<undef> if not in a subroutine |- | Flow || [[#wantarray|wantarray]] || get void vs scalar vs list context of current subroutine call |- | HASH || [[#delete|delete]] || deletes a value from a hash |- | HASH || [[#each|each]] || retrieve the next key/value pair from a hash |- | HASH || [[#exists|exists]] || test whether a hash key is present |- | HASH || [[#keys|keys]] || retrieve list of indices from a hash |- | HASH || [[#values|values]] || return a list of the values in a hash |- | I/O || [[#binmode|binmode]] || prepare binary files for I/O |- | I/O || [[#close|close]] || close file (or pipe or socket) handle |- | I/O || [[#closedir|closedir]] || close directory handle |- | I/O || [[#dbmclose|dbmclose]] || breaks binding on a tied dbm file |- | I/O || [[#dbmopen|dbmopen]] || create binding on a tied dbm file |- | I/O || [[#die|die]] || raise an exception or bail out |- | I/O || [[#eof|eof]] || test a filehandle for its end |- | I/O || [[#fileno|fileno]] || return file descriptor from filehandle |- | I/O || [[#flock|flock]] || lock an entire file with an advisory lock |- | I/O || [[#format|format]] || declare a picture format with use by the write() function |- | I/O || [[#getc|getc]] || get the next character from the filehandle |- | I/O || [[#print|print]] || output a list to a filehandle |- | I/O || [[#printf|printf]] || output a formatted list to a filehandle |- | I/O || [[#read|read]] || fixed-length buffered input from a filehandle |- | I/O || [[#readdir|readdir]] || get a directory from a directory handle |- | I/O || [[#readline|readline]] || fetch a record from a file |- | I/O || [[#rewinddir|rewinddir]] || reset directory handle |- | I/O || [[#say|say]] || output a list to a filehandle, appending a newline |- | I/O || [[#seek|seek]] || reposition file pointer for random-access I/O |- | I/O || [[#seekdir|seekdir]] || reposition directory pointer |- | I/O || [[#select|select]] || reset default output or do I/O multiplexing |- | I/O || [[#syscall|syscall]] || execute an arbitrary system call |- | I/O || [[#sysread|sysread]] || fixed-length unbuffered input from a filehandle |- | I/O || [[#sysseek|sysseek]] || position I/O pointer on handle used with sysread and syswrite |- | I/O || [[#syswrite|syswrite]] || fixed-length unbuffered output to a filehandle |- | I/O || [[#tell|tell]] || get current seekpointer on a filehandle |- | I/O || [[#telldir|telldir]] || get current seekpointer on a directory handle |- | I/O || [[#truncate|truncate]] || shorten a file |- | I/O || [[#warn|warn]] || print debugging info |- | I/O || [[#write|write]] || print a picture record |- | LIST || [[#grep|grep]] || locate elements in a list test true against a given criterion |- | LIST || [[#join|join]] || join a list into a string using a separator |- | LIST || [[#map|map]] || apply a change to a list to get back a new list with the changes |- | LIST || [[#qw/STRING/|qw/STRING/]] || quote a list of words |- | LIST || [[#reverse|reverse]] || flip a string or a list |- | LIST || [[#sort|sort]] || sort a list of values |- | LIST || [[#unpack|unpack]] || convert binary structure into normal perl variables |- | Math || [[#abs|abs]] || absolute value function |- | Math || [[#atan2|atan2]] || arctangent of Y/X in the range -PI to PI |- | Math || [[#cos|cos]] || cosine function |- | Math || [[#exp|exp]] || raise I<e> to a power |- | Math || [[#hex|hex]] || convert a hexadecimal string to a number |- | Math || [[#int|int]] || get the integer portion of a number |- | Math || [[#log|log]] || retrieve the natural logarithm for a number |- | Math || [[#oct|oct]] || convert a string to an octal number |- | Math || [[#rand|rand]] || retrieve the next pseudorandom number |- | Math || [[#sin|sin]] || return the sine of a number |- | Math || [[#sqrt|sqrt]] || square root function |- | Math || [[#srand|srand]] || seed the random number generator |- | Misc || [[#defined|defined]] || test whether a value, variable, or function is defined |- | Misc || [[#formline|formline]] || internal function used for formats |- | Misc || [[#lock|lock]] || get a thread lock on a variable, subroutine, or method |- | Misc || [[#prototype|prototype]] || get the prototype (if any) of a subroutine |- | Misc || [[#reset|reset]] || clear all variables of a given name |- | Misc || [[#scalar|scalar]] || force a scalar context |- | Misc || [[#undef|undef]] || remove a variable or function definition |- | Modules || [[#do|do]] || turn a BLOCK into a TERM |- | Modules || [[#import|import]] || patch a module's namespace into your own |- | Modules || [[#no|no]] || unimport some module symbols or semantics at compile time |- | Modules || [[#package|package]] || declare a separate global namespace |- | Modules || [[#require|require]] || load in external functions from a library at runtime |- | Modules || [[#use|use]] || load in a module at compile time and import its namespace |- | Namespace || [[#caller|caller]] || get context of the current subroutine call |- | Namespace || [[#import|import]] || patch a module's namespace into your own |- | Namespace || [[#local|local]] || create a temporary value for a global variable (dynamic scoping) |- | Namespace || [[#my|my]] || declare and assign a local variable (lexical scoping) |- | Namespace || [[#our|our]] || declare and assign a package variable (lexical scoping) |- | Namespace || [[#package|package]] || declare a separate global namespace |- | Namespace || [[#state|state]] || declare and assign a persistent lexical variable |- | Namespace || [[#use|use]] || load in a module at compile time and import its namespace |- | Network || [[#endprotoent|endprotoent]] || be done using protocols file |- | Network || [[#endservent|endservent]] || be done using services file |- | Network || [[#gethostbyaddr|gethostbyaddr]] || get host record given its address |- | Network || [[#gethostbyname|gethostbyname]] || get host record given name |- | Network || [[#gethostent|gethostent]] || get next hosts record |- | Network || [[#getnetbyaddr|getnetbyaddr]] || get network record given its address |- | Network || [[#getnetbyname|getnetbyname]] || get networks record given name |- | Network || [[#getnetent|getnetent]] || get next networks record |- | Network || [[#getprotobyname|getprotobyname]] || get protocol record given name |- | Network || [[#getprotobynumber|getprotobynumber]] || get protocol record numeric protocol |- | Network || [[#getprotoent|getprotoent]] || get next protocols record |- | Network || [[#getservbyname|getservbyname]] || get services record given its name |- | Network || [[#getservbyport|getservbyport]] || get services record given numeric port |- | Network || [[#getservent|getservent]] || get next services record |- | Network || [[#sethostent|sethostent]] || prepare hosts file for use |- | Network || [[#setnetent|setnetent]] || prepare networks file for use |- | Network || [[#setprotoent|setprotoent]] || prepare protocols file for use |- | Network || [[#setservent|setservent]] || prepare services file for use |- | Objects || [[#bless|bless]] || create an object |- | Objects || [[#dbmclose|dbmclose]] || breaks binding on a tied dbm file |- | Objects || [[#dbmopen|dbmopen]] || create binding on a tied dbm file |- | Objects || [[#package|package]] || declare a separate global namespace |- | Objects || [[#ref|ref]] || find out the type of thing being referenced |- | Objects || [[#tie|tie]] || bind a variable to an object class |- | Objects || [[#tied|tied]] || get a reference to the object underlying a tied variable |- | Objects || [[#untie|untie]] || break a tie binding to a variable |- | Objects || [[#use|use]] || load in a module at compile time and import its namespace |- | Process || [[#alarm|alarm]] || schedule a SIGALRM |- | Process || [[#exec|exec]] || abandon this program to run another |- | Process || [[#fork|fork]] || create a new process just like this one |- | Process || [[#getpgrp|getpgrp]] || get process group |- | Process || [[#getppid|getppid]] || get parent process ID |- | Process || [[#getpriority|getpriority]] || get current nice value |- | Process || [[#kill|kill]] || send a signal to a process or process group |- | Process || [[#pipe|pipe]] || open a pair of connected filehandles |- | Process || [[#qx/STRING/|qx/STRING/]] || backquote quote a string |- | Process || [[#readpipe|readpipe]] || execute a system command and collect standard output |- | Process || [[#setpgrp|setpgrp]] || set the process group of a process |- | Process || [[#setpriority|setpriority]] || set a process's nice value |- | Process || [[#sleep|sleep]] || block for some number of seconds |- | Process || [[#system|system]] || run a separate program |- | Process || [[#times|times]] || return elapsed time for self and child processes |- | Process || [[#wait|wait]] || wait for any child process to die |- | Process || [[#waitpid|waitpid]] || wait for a particular child process to die |- | Regexp || [[#m//|m//]] || match a string with a regular expression pattern |- | Regexp || [[#pos|pos]] || find or set the offset for the last/next m//g search |- | Regexp || [[#qr/STRING/|qr/STRING/]] || compile pattern |- | Regexp || [[#quotemeta|quotemeta]] || quote regular expression magic characters |- | Regexp || [[#s///|s///]] || replace a pattern with a string |- | Regexp || [[#split|split]] || split up a string using a regexp delimiter |- | Regexp || [[#study|study]] || no-op, formerly optimized input data for repeated searches |- | Socket || [[#accept|accept]] || accept an incoming socket connect |- | Socket || [[#bind|bind]] || binds an address to a socket |- | Socket || [[#connect|connect]] || connect to a remote socket |- | Socket || [[#getpeername|getpeername]] || find the other end of a socket connection |- | Socket || [[#getsockname|getsockname]] || retrieve the sockaddr for a given socket |- | Socket || [[#getsockopt|getsockopt]] || get socket options on a given socket |- | Socket || [[#listen|listen]] || register your socket as a server |- | Socket || [[#recv|recv]] || receive a message over a Socket |- | Socket || [[#send|send]] || send a message over a socket |- | Socket || [[#setsockopt|setsockopt]] || set some socket options |- | Socket || [[#shutdown|shutdown]] || close down just half of a socket connection |- | Socket || [[#socket|socket]] || create a socket |- | Socket || [[#socketpair|socketpair]] || create a pair of sockets |- | String || [[#chomp|chomp]] || remove a trailing record separator from a string |- | String || [[#chop|chop]] || remove the last character from a string |- | String || [[#chr|chr]] || get character this number represents |- | String || [[#crypt|crypt]] || one-way passwd-style encryption |- | String || [[#fc|fc]] || return casefolded version of a string |- | String || [[#hex|hex]] || convert a hexadecimal string to a number |- | String || [[#index|index]] || find a substring within a string |- | String || [[#lc|lc]] || return lower-case version of a string |- | String || [[#lcfirst|lcfirst]] || return a string with just the next letter in lower case |- | String || [[#length|length]] || return the number of characters in a string |- | String || [[#oct|oct]] || convert a string to an octal number |- | String || [[#ord|ord]] || find a character's numeric representation |- | String || [[#pack|pack]] || convert a list into a binary representation |- | String || [[#q/STRING/|q/STRING/]] || singly quote a string |- | String || [[#qq/STRING/|qq/STRING/]] || doubly quote a string |- | String || [[#reverse|reverse]] || flip a string or a list |- | String || [[#rindex|rindex]] || right-to-left substring search |- | String || [[#sprintf|sprintf]] || formatted print into a string |- | String || [[#substr|substr]] || get or alter a portion of a string |- | String || [[#tr///|tr///]] || transliterate a string |- | String || [[#uc|uc]] || return upper-case version of a string |- | String || [[#ucfirst|ucfirst]] || return a string with just the next letter in upper case |- | String || [[#y///|y///]] || transliterate a string |- | SysV || [[#msgctl|msgctl]] || SysV IPC message control operations |- | SysV || [[#msgget|msgget]] || get SysV IPC message queue |- | SysV || [[#msgrcv|msgrcv]] || receive a SysV IPC message from a message queue |- | SysV || [[#msgsnd|msgsnd]] || send a SysV IPC message to a message queue |- | SysV || [[#semctl|semctl]] || SysV semaphore control operations |- | SysV || [[#semget|semget]] || get set of SysV semaphores |- | SysV || [[#semop|semop]] || SysV semaphore operations |- | SysV || [[#shmctl|shmctl]] || SysV shared memory operations |- | SysV || [[#shmget|shmget]] || get SysV shared memory segment identifier |- | SysV || [[#shmread|shmread]] || read SysV shared memory |- | SysV || [[#shmwrite|shmwrite]] || write SysV shared memory |- | Time || [[#gmtime|gmtime]] || convert UNIX time into record or string using Greenwich time |- | Time || [[#localtime|localtime]] || convert UNIX time into record or string using local time |- | Time || [[#time|time]] || return number of seconds since 1970 |- | Time || [[#times|times]] || return elapsed time for self and child processes |- | User || [[#endgrent|endgrent]] || be done using group file |- | User || [[#endhostent|endhostent]] || be done using hosts file |- | User || [[#endnetent|endnetent]] || be done using networks file |- | User || [[#endpwent|endpwent]] || be done using passwd file |- | User || [[#getgrent|getgrent]] || get next group record |- | User || [[#getgrgid|getgrgid]] || get group record given group user ID |- | User || [[#getgrnam|getgrnam]] || get group record given group name |- | User || [[#getlogin|getlogin]] || return who logged in at this tty |- | User || [[#getpwent|getpwent]] || get next passwd record |- | User || [[#getpwnam|getpwnam]] || get passwd record given user login name |- | User || [[#getpwuid|getpwuid]] || get passwd record given user ID |- | User || [[#setgrent|setgrent]] || prepare group file for use |- | User || [[#setpwent|setpwent]] || prepare passwd file for use |} --> == 組込み関数一覧 == {{Main|[https://perldoc.perl.org/perlfunc perlfunc(en)]|[https://perldoc.jp/docs/perl/perlfunc.pod perldoc(ja)]}} <!-- <syntaxhighlight lang=perl copy> use v5.30.0; use Pod::Functions; # Kind -> Function -> Flavor my @kkeys = sort keys %Kinds; foreach my $kkey(@kkeys) { my ($kind, $functions) = %Kinds{$kkey}; print <<EOS; === $kind === @{[ map { "[[#$_|$_]]" } @$functions ]} EOS foreach my $function(@$functions) { my ($null, $flaver) = %Flavor{$function}; print <<EOS; ==== $function ==== @{[ $flaver ]} {{See also|[https://perldoc.perl.org/functions/$function $function(en)]|[https://perldoc.jp/func/$function $function(ja)]}} EOS } } </syntaxhighlight> --> === ARRAY === [[#each|each]] [[#keys|keys]] [[#pop|pop]] [[#push|push]] [[#shift|shift]] [[#splice|splice]] [[#unshift|unshift]] [[#values|values]] ==== each ==== retrieve the next key/value pair from a hash {{See also|[https://perldoc.perl.org/functions/each each(en)]|[https://perldoc.jp/func/each each(ja)]}} ==== keys ==== retrieve list of indices from a hash {{See also|[https://perldoc.perl.org/functions/keys keys(en)]|[https://perldoc.jp/func/keys keys(ja)]}} ==== pop ==== remove the last element from an array and return it {{See also|[https://perldoc.perl.org/functions/pop pop(en)]|[https://perldoc.jp/func/pop pop(ja)]}} ==== push ==== append one or more elements to an array {{See also|[https://perldoc.perl.org/functions/push push(en)]|[https://perldoc.jp/func/push push(ja)]}} ==== shift ==== remove the first element of an array, and return it {{See also|[https://perldoc.perl.org/functions/shift shift(en)]|[https://perldoc.jp/func/shift shift(ja)]}} ==== splice ==== add or remove elements anywhere in an array {{See also|[https://perldoc.perl.org/functions/splice splice(en)]|[https://perldoc.jp/func/splice splice(ja)]}} ==== unshift ==== prepend more elements to the beginning of a list {{See also|[https://perldoc.perl.org/functions/unshift unshift(en)]|[https://perldoc.jp/func/unshift unshift(ja)]}} ==== values ==== return a list of the values in a hash {{See also|[https://perldoc.perl.org/functions/values values(en)]|[https://perldoc.jp/func/values values(ja)]}} === Binary === [[#pack|pack]] [[#read|read]] [[#syscall|syscall]] [[#sysread|sysread]] [[#sysseek|sysseek]] [[#syswrite|syswrite]] [[#unpack|unpack]] [[#vec|vec]] ==== pack ==== convert a list into a binary representation {{See also|[https://perldoc.perl.org/functions/pack pack(en)]|[https://perldoc.jp/func/pack pack(ja)]}} ==== read ==== fixed-length buffered input from a filehandle {{See also|[https://perldoc.perl.org/functions/read read(en)]|[https://perldoc.jp/func/read read(ja)]}} ==== syscall ==== execute an arbitrary system call {{See also|[https://perldoc.perl.org/functions/syscall syscall(en)]|[https://perldoc.jp/func/syscall syscall(ja)]}} ==== sysread ==== fixed-length unbuffered input from a filehandle {{See also|[https://perldoc.perl.org/functions/sysread sysread(en)]|[https://perldoc.jp/func/sysread sysread(ja)]}} ==== sysseek ==== position I/O pointer on handle used with sysread and syswrite {{See also|[https://perldoc.perl.org/functions/sysseek sysseek(en)]|[https://perldoc.jp/func/sysseek sysseek(ja)]}} ==== syswrite ==== fixed-length unbuffered output to a filehandle {{See also|[https://perldoc.perl.org/functions/syswrite syswrite(en)]|[https://perldoc.jp/func/syswrite syswrite(ja)]}} ==== unpack ==== Perlの<code>unpack</code>関数は、指定されたフォーマットに従ってバイナリ文字列をアンパックしてデータを取得します。主にバイナリデータを処理する際に使用されます。以下は、<code>unpack</code>関数の基本的な使用例と一般的なフォーマット指定子の一部です。 :<syntaxhighlight lang=perl copy> # バイナリ文字列を作成する例 my $binary_data = "ABCDHello"; # バイナリ文字列をアンパックしてデータを取得する my ($num1, $num2, $string) = unpack('C4 A4', $binary_data); print "$num1 $num2 $string\n"; #=> 65 66 67 </syntaxhighlight> 一般的なフォーマット指定子の一部は以下の通りです。 * <code>A</code> - 文字列 * <code>C</code> - 符号なし8ビット整数 (1 バイト) * <code>S</code> - 符号なし16ビット整数 (2 バイト) * <code>L</code> - 符号なし32ビット整数 (4 バイト) * <code>n</code> - ネットワークバイトオーダー (big-endian) の16ビット整数 * <code>N</code> - ネットワークバイトオーダーの32ビット整数 * <code>v</code> - リトルエンディアンバイトオーダーの16ビット整数 * <code>V</code> - リトルエンディアンバイトオーダーの32ビット整数 <code>unpack</code>関数は、バイナリデータを他の形式に変換する際に非常に便利です。 {{See also|[[#pack|pack]]|[https://perldoc.perl.org/functions/unpack unpack(en)]|[https://perldoc.jp/func/unpack unpack(ja)]}} ==== vec ==== test or set particular bits in a string {{See also|[https://perldoc.perl.org/functions/vec vec(en)]|[https://perldoc.jp/func/vec vec(ja)]}} === File === [[#-X|-X]] [[#chdir|chdir]] [[#chmod|chmod]] [[#chown|chown]] [[#chroot|chroot]] [[#fcntl|fcntl]] [[#glob|glob]] [[#ioctl|ioctl]] [[#link|link]] [[#lstat|lstat]] [[#mkdir|mkdir]] [[#open|open]] [[#opendir|opendir]] [[#readlink|readlink]] [[#rename|rename]] [[#rmdir|rmdir]] [[#select|select]] [[#stat|stat]] [[#symlink|symlink]] [[#sysopen|sysopen]] [[#umask|umask]] [[#unlink|unlink]] [[#utime|utime]] ==== -X ==== a file test (-r, -x, etc) {{See also|[https://perldoc.perl.org/functions/-X -X(en)]|[https://perldoc.jp/func/-X -X(ja)]}} ==== chdir ==== change your current working directory {{See also|[https://perldoc.perl.org/functions/chdir chdir(en)]|[https://perldoc.jp/func/chdir chdir(ja)]}} ==== chmod ==== changes the permissions on a list of files {{See also|[https://perldoc.perl.org/functions/chmod chmod(en)]|[https://perldoc.jp/func/chmod chmod(ja)]}} ==== chown ==== change the ownership on a list of files {{See also|[https://perldoc.perl.org/functions/chown chown(en)]|[https://perldoc.jp/func/chown chown(ja)]}} ==== chroot ==== make directory new root for path lookups {{See also|[https://perldoc.perl.org/functions/chroot chroot(en)]|[https://perldoc.jp/func/chroot chroot(ja)]}} ==== fcntl ==== file control system call {{See also|[https://perldoc.perl.org/functions/fcntl fcntl(en)]|[https://perldoc.jp/func/fcntl fcntl(ja)]}} ==== glob ==== expand filenames using wildcards {{See also|[https://perldoc.perl.org/functions/glob glob(en)]|[https://perldoc.jp/func/glob glob(ja)]}} ==== ioctl ==== system-dependent device control system call {{See also|[https://perldoc.perl.org/functions/ioctl ioctl(en)]|[https://perldoc.jp/func/ioctl ioctl(ja)]}} ==== link ==== create a hard link in the filesystem {{See also|[https://perldoc.perl.org/functions/link link(en)]|[https://perldoc.jp/func/link link(ja)]}} ==== lstat ==== stat a symbolic link {{See also|[https://perldoc.perl.org/functions/lstat lstat(en)]|[https://perldoc.jp/func/lstat lstat(ja)]}} ==== mkdir ==== create a directory {{See also|[https://perldoc.perl.org/functions/mkdir mkdir(en)]|[https://perldoc.jp/func/mkdir mkdir(ja)]}} ==== open ==== open a file, pipe, or descriptor {{See also|[https://perldoc.perl.org/functions/open open(en)]|[https://perldoc.jp/func/open open(ja)]}} ==== opendir ==== open a directory {{See also|[https://perldoc.perl.org/functions/opendir opendir(en)]|[https://perldoc.jp/func/opendir opendir(ja)]}} ==== readlink ==== determine where a symbolic link is pointing {{See also|[https://perldoc.perl.org/functions/readlink readlink(en)]|[https://perldoc.jp/func/readlink readlink(ja)]}} ==== rename ==== change a filename {{See also|[https://perldoc.perl.org/functions/rename rename(en)]|[https://perldoc.jp/func/rename rename(ja)]}} ==== rmdir ==== remove a directory {{See also|[https://perldoc.perl.org/functions/rmdir rmdir(en)]|[https://perldoc.jp/func/rmdir rmdir(ja)]}} ==== select ==== reset default output or do I/O multiplexing {{See also|[https://perldoc.perl.org/functions/select select(en)]|[https://perldoc.jp/func/select select(ja)]}} ==== stat ==== get a file's status information {{See also|[https://perldoc.perl.org/functions/stat stat(en)]|[https://perldoc.jp/func/stat stat(ja)]}} ==== symlink ==== create a symbolic link to a file {{See also|[https://perldoc.perl.org/functions/symlink symlink(en)]|[https://perldoc.jp/func/symlink symlink(ja)]}} ==== sysopen ==== open a file, pipe, or descriptor {{See also|[https://perldoc.perl.org/functions/sysopen sysopen(en)]|[https://perldoc.jp/func/sysopen sysopen(ja)]}} ==== umask ==== set file creation mode mask {{See also|[https://perldoc.perl.org/functions/umask umask(en)]|[https://perldoc.jp/func/umask umask(ja)]}} ==== unlink ==== remove one link to a file {{See also|[https://perldoc.perl.org/functions/unlink unlink(en)]|[https://perldoc.jp/func/unlink unlink(ja)]}} ==== utime ==== set a file's last access and modify times {{See also|[https://perldoc.perl.org/functions/utime utime(en)]|[https://perldoc.jp/func/utime utime(ja)]}} === Flow === [[#break|break]] [[#caller|caller]] [[#continue|continue]] [[#die|die]] [[#do|do]] [[#dump|dump]] [[#eval|eval]] [[#evalbytes|evalbytes]] [[#exit|exit]] [[#__FILE__|__FILE__]] [[#goto|goto]] [[#last|last]] [[#__LINE__|__LINE__]] [[#next|next]] [[#__PACKAGE__|__PACKAGE__]] [[#redo|redo]] [[#return|return]] [[#sub|sub]] [[#__SUB__|__SUB__]] [[#wantarray|wantarray]] ==== break ==== break out of a C<given> block {{See also|[https://perldoc.perl.org/functions/break break(en)]|[https://perldoc.jp/func/break break(ja)]}} ==== caller ==== get context of the current subroutine call {{See also|[https://perldoc.perl.org/functions/caller caller(en)]|[https://perldoc.jp/func/caller caller(ja)]}} ==== continue ==== optional trailing block in a while or foreach {{See also|[https://perldoc.perl.org/functions/continue continue(en)]|[https://perldoc.jp/func/continue continue(ja)]}} ==== die ==== raise an exception or bail out {{See also|[https://perldoc.perl.org/functions/die die(en)]|[https://perldoc.jp/func/die die(ja)]}} ==== do ==== turn a BLOCK into a TERM {{See also|[https://perldoc.perl.org/functions/do do(en)]|[https://perldoc.jp/func/do do(ja)]}} ==== dump ==== create an immediate core dump {{See also|[https://perldoc.perl.org/functions/dump dump(en)]|[https://perldoc.jp/func/dump dump(ja)]}} ==== eval ==== catch exceptions or compile and run code {{See also|[https://perldoc.perl.org/functions/eval eval(en)]|[https://perldoc.jp/func/eval eval(ja)]}} ==== evalbytes ==== similar to string eval, but intend to parse a bytestream {{See also|[https://perldoc.perl.org/functions/evalbytes evalbytes(en)]|[https://perldoc.jp/func/evalbytes evalbytes(ja)]}} ==== exit ==== terminate this program {{See also|[https://perldoc.perl.org/functions/exit exit(en)]|[https://perldoc.jp/func/exit exit(ja)]}} ==== __FILE__ ==== the name of the current source file {{See also|[https://perldoc.perl.org/functions/__FILE__ __FILE__(en)]|[https://perldoc.jp/func/__FILE__ __FILE__(ja)]}} ==== goto ==== create spaghetti code {{See also|[https://perldoc.perl.org/functions/goto goto(en)]|[https://perldoc.jp/func/goto goto(ja)]}} ==== last ==== exit a block prematurely {{See also|[https://perldoc.perl.org/functions/last last(en)]|[https://perldoc.jp/func/last last(ja)]}} ==== __LINE__ ==== the current source line number {{See also|[https://perldoc.perl.org/functions/__LINE__ __LINE__(en)]|[https://perldoc.jp/func/__LINE__ __LINE__(ja)]}} ==== next ==== iterate a block prematurely {{See also|[https://perldoc.perl.org/functions/next next(en)]|[https://perldoc.jp/func/next next(ja)]}} ==== __PACKAGE__ ==== the current package {{See also|[https://perldoc.perl.org/functions/__PACKAGE__ __PACKAGE__(en)]|[https://perldoc.jp/func/__PACKAGE__ __PACKAGE__(ja)]}} ==== redo ==== start this loop iteration over again {{See also|[https://perldoc.perl.org/functions/redo redo(en)]|[https://perldoc.jp/func/redo redo(ja)]}} ==== return ==== get out of a function early {{See also|[https://perldoc.perl.org/functions/return return(en)]|[https://perldoc.jp/func/return return(ja)]}} ==== sub ==== declare a subroutine, possibly anonymously {{See also|[https://perldoc.perl.org/functions/sub sub(en)]|[https://perldoc.jp/func/sub sub(ja)]}} ==== __SUB__ ==== the current subroutine, or C<undef> if not in a subroutine {{See also|[https://perldoc.perl.org/functions/__SUB__ __SUB__(en)]|[https://perldoc.jp/func/__SUB__ __SUB__(ja)]}} ==== wantarray ==== get void vs scalar vs list context of current subroutine call {{See also|[https://perldoc.perl.org/functions/wantarray wantarray(en)]|[https://perldoc.jp/func/wantarray wantarray(ja)]}} === HASH === [[#delete|delete]] [[#each|each]] [[#exists|exists]] [[#keys|keys]] [[#values|values]] ==== delete ==== deletes a value from a hash {{See also|[https://perldoc.perl.org/functions/delete delete(en)]|[https://perldoc.jp/func/delete delete(ja)]}} ==== each ==== retrieve the next key/value pair from a hash {{See also|[https://perldoc.perl.org/functions/each each(en)]|[https://perldoc.jp/func/each each(ja)]}} ==== exists ==== test whether a hash key is present {{See also|[https://perldoc.perl.org/functions/exists exists(en)]|[https://perldoc.jp/func/exists exists(ja)]}} ==== keys ==== retrieve list of indices from a hash {{See also|[https://perldoc.perl.org/functions/keys keys(en)]|[https://perldoc.jp/func/keys keys(ja)]}} ==== values ==== return a list of the values in a hash {{See also|[https://perldoc.perl.org/functions/values values(en)]|[https://perldoc.jp/func/values values(ja)]}} === I/O === [[#binmode|binmode]] [[#close|close]] [[#closedir|closedir]] [[#dbmclose|dbmclose]] [[#dbmopen|dbmopen]] [[#die|die]] [[#eof|eof]] [[#fileno|fileno]] [[#flock|flock]] [[#format|format]] [[#getc|getc]] [[#print|print]] [[#printf|printf]] [[#read|read]] [[#readdir|readdir]] [[#readline|readline]] [[#rewinddir|rewinddir]] [[#say|say]] [[#seek|seek]] [[#seekdir|seekdir]] [[#select|select]] [[#syscall|syscall]] [[#sysread|sysread]] [[#sysseek|sysseek]] [[#syswrite|syswrite]] [[#tell|tell]] [[#telldir|telldir]] [[#truncate|truncate]] [[#warn|warn]] [[#write|write]] ==== binmode ==== prepare binary files for I/O {{See also|[https://perldoc.perl.org/functions/binmode binmode(en)]|[https://perldoc.jp/func/binmode binmode(ja)]}} ==== close ==== close file (or pipe or socket) handle {{See also|[https://perldoc.perl.org/functions/close close(en)]|[https://perldoc.jp/func/close close(ja)]}} ==== closedir ==== close directory handle {{See also|[https://perldoc.perl.org/functions/closedir closedir(en)]|[https://perldoc.jp/func/closedir closedir(ja)]}} ==== dbmclose ==== breaks binding on a tied dbm file {{See also|[https://perldoc.perl.org/functions/dbmclose dbmclose(en)]|[https://perldoc.jp/func/dbmclose dbmclose(ja)]}} ==== dbmopen ==== create binding on a tied dbm file {{See also|[https://perldoc.perl.org/functions/dbmopen dbmopen(en)]|[https://perldoc.jp/func/dbmopen dbmopen(ja)]}} ==== die ==== raise an exception or bail out {{See also|[https://perldoc.perl.org/functions/die die(en)]|[https://perldoc.jp/func/die die(ja)]}} ==== eof ==== test a filehandle for its end {{See also|[https://perldoc.perl.org/functions/eof eof(en)]|[https://perldoc.jp/func/eof eof(ja)]}} ==== fileno ==== return file descriptor from filehandle {{See also|[https://perldoc.perl.org/functions/fileno fileno(en)]|[https://perldoc.jp/func/fileno fileno(ja)]}} ==== flock ==== lock an entire file with an advisory lock {{See also|[https://perldoc.perl.org/functions/flock flock(en)]|[https://perldoc.jp/func/flock flock(ja)]}} ==== format ==== declare a picture format with use by the write() function {{See also|[https://perldoc.perl.org/functions/format format(en)]|[https://perldoc.jp/func/format format(ja)]}} ==== getc ==== get the next character from the filehandle {{See also|[https://perldoc.perl.org/functions/getc getc(en)]|[https://perldoc.jp/func/getc getc(ja)]}} ==== print ==== output a list to a filehandle {{See also|[https://perldoc.perl.org/functions/print print(en)]|[https://perldoc.jp/func/print print(ja)]}} ==== printf ==== output a formatted list to a filehandle {{See also|[https://perldoc.perl.org/functions/printf printf(en)]|[https://perldoc.jp/func/printf printf(ja)]}} ==== read ==== fixed-length buffered input from a filehandle {{See also|[https://perldoc.perl.org/functions/read read(en)]|[https://perldoc.jp/func/read read(ja)]}} ==== readdir ==== get a directory from a directory handle {{See also|[https://perldoc.perl.org/functions/readdir readdir(en)]|[https://perldoc.jp/func/readdir readdir(ja)]}} ==== readline ==== fetch a record from a file {{See also|[https://perldoc.perl.org/functions/readline readline(en)]|[https://perldoc.jp/func/readline readline(ja)]}} ==== rewinddir ==== reset directory handle {{See also|[https://perldoc.perl.org/functions/rewinddir rewinddir(en)]|[https://perldoc.jp/func/rewinddir rewinddir(ja)]}} ==== say ==== output a list to a filehandle, appending a newline {{See also|[https://perldoc.perl.org/functions/say say(en)]|[https://perldoc.jp/func/say say(ja)]}} ==== seek ==== reposition file pointer for random-access I/O {{See also|[https://perldoc.perl.org/functions/seek seek(en)]|[https://perldoc.jp/func/seek seek(ja)]}} ==== seekdir ==== reposition directory pointer {{See also|[https://perldoc.perl.org/functions/seekdir seekdir(en)]|[https://perldoc.jp/func/seekdir seekdir(ja)]}} ==== select ==== reset default output or do I/O multiplexing {{See also|[https://perldoc.perl.org/functions/select select(en)]|[https://perldoc.jp/func/select select(ja)]}} ==== syscall ==== execute an arbitrary system call {{See also|[https://perldoc.perl.org/functions/syscall syscall(en)]|[https://perldoc.jp/func/syscall syscall(ja)]}} ==== sysread ==== fixed-length unbuffered input from a filehandle {{See also|[https://perldoc.perl.org/functions/sysread sysread(en)]|[https://perldoc.jp/func/sysread sysread(ja)]}} ==== sysseek ==== position I/O pointer on handle used with sysread and syswrite {{See also|[https://perldoc.perl.org/functions/sysseek sysseek(en)]|[https://perldoc.jp/func/sysseek sysseek(ja)]}} ==== syswrite ==== fixed-length unbuffered output to a filehandle {{See also|[https://perldoc.perl.org/functions/syswrite syswrite(en)]|[https://perldoc.jp/func/syswrite syswrite(ja)]}} ==== tell ==== get current seekpointer on a filehandle {{See also|[https://perldoc.perl.org/functions/tell tell(en)]|[https://perldoc.jp/func/tell tell(ja)]}} ==== telldir ==== get current seekpointer on a directory handle {{See also|[https://perldoc.perl.org/functions/telldir telldir(en)]|[https://perldoc.jp/func/telldir telldir(ja)]}} ==== truncate ==== shorten a file {{See also|[https://perldoc.perl.org/functions/truncate truncate(en)]|[https://perldoc.jp/func/truncate truncate(ja)]}} ==== warn ==== print debugging info {{See also|[https://perldoc.perl.org/functions/warn warn(en)]|[https://perldoc.jp/func/warn warn(ja)]}} ==== write ==== print a picture record {{See also|[https://perldoc.perl.org/functions/write write(en)]|[https://perldoc.jp/func/write write(ja)]}} === LIST === [[#grep|grep]] [[#join|join]] [[#map|map]] [[#qw/STRING/|qw/STRING/]] [[#reverse|reverse]] [[#sort|sort]] [[#unpack|unpack]] ==== grep ==== locate elements in a list test true against a given criterion {{See also|[https://perldoc.perl.org/functions/grep grep(en)]|[https://perldoc.jp/func/grep grep(ja)]}} ==== join ==== join a list into a string using a separator {{See also|[https://perldoc.perl.org/functions/join join(en)]|[https://perldoc.jp/func/join join(ja)]}} ==== map ==== apply a change to a list to get back a new list with the changes {{See also|[https://perldoc.perl.org/functions/map map(en)]|[https://perldoc.jp/func/map map(ja)]}} ==== qw/STRING/ ==== quote a list of words {{See also|[https://perldoc.perl.org/functions/qw/STRING/ qw/STRING/(en)]|[https://perldoc.jp/func/qw/STRING/ qw/STRING/(ja)]}} ==== reverse ==== flip a string or a list {{See also|[https://perldoc.perl.org/functions/reverse reverse(en)]|[https://perldoc.jp/func/reverse reverse(ja)]}} ==== sort ==== sort a list of values {{See also|[https://perldoc.perl.org/functions/sort sort(en)]|[https://perldoc.jp/func/sort sort(ja)]}} ==== unpack ==== convert binary structure into normal perl variables {{See also|[https://perldoc.perl.org/functions/unpack unpack(en)]|[https://perldoc.jp/func/unpack unpack(ja)]}} === Math === [[#abs|abs]] [[#atan2|atan2]] [[#cos|cos]] [[#exp|exp]] [[#hex|hex]] [[#int|int]] [[#log|log]] [[#oct|oct]] [[#rand|rand]] [[#sin|sin]] [[#sqrt|sqrt]] [[#srand|srand]] ==== abs ==== absolute value function {{See also|[https://perldoc.perl.org/functions/abs abs(en)]|[https://perldoc.jp/func/abs abs(ja)]}} ==== atan2 ==== arctangent of Y/X in the range -PI to PI {{See also|[https://perldoc.perl.org/functions/atan2 atan2(en)]|[https://perldoc.jp/func/atan2 atan2(ja)]}} ==== cos ==== cosine function {{See also|[https://perldoc.perl.org/functions/cos cos(en)]|[https://perldoc.jp/func/cos cos(ja)]}} ==== exp ==== raise I<e> to a power {{See also|[https://perldoc.perl.org/functions/exp exp(en)]|[https://perldoc.jp/func/exp exp(ja)]}} ==== hex ==== convert a hexadecimal string to a number {{See also|[https://perldoc.perl.org/functions/hex hex(en)]|[https://perldoc.jp/func/hex hex(ja)]}} ==== int ==== get the integer portion of a number {{See also|[https://perldoc.perl.org/functions/int int(en)]|[https://perldoc.jp/func/int int(ja)]}} ==== log ==== retrieve the natural logarithm for a number {{See also|[https://perldoc.perl.org/functions/log log(en)]|[https://perldoc.jp/func/log log(ja)]}} ==== oct ==== convert a string to an octal number {{See also|[https://perldoc.perl.org/functions/oct oct(en)]|[https://perldoc.jp/func/oct oct(ja)]}} ==== rand ==== retrieve the next pseudorandom number {{See also|[https://perldoc.perl.org/functions/rand rand(en)]|[https://perldoc.jp/func/rand rand(ja)]}} ==== sin ==== return the sine of a number {{See also|[https://perldoc.perl.org/functions/sin sin(en)]|[https://perldoc.jp/func/sin sin(ja)]}} ==== sqrt ==== square root function {{See also|[https://perldoc.perl.org/functions/sqrt sqrt(en)]|[https://perldoc.jp/func/sqrt sqrt(ja)]}} ==== srand ==== seed the random number generator {{See also|[https://perldoc.perl.org/functions/srand srand(en)]|[https://perldoc.jp/func/srand srand(ja)]}} === Misc === [[#defined|defined]] [[#formline|formline]] [[#lock|lock]] [[#prototype|prototype]] [[#reset|reset]] [[#scalar|scalar]] [[#undef|undef]] ==== defined ==== test whether a value, variable, or function is defined {{See also|[https://perldoc.perl.org/functions/defined defined(en)]|[https://perldoc.jp/func/defined defined(ja)]}} ==== formline ==== internal function used for formats {{See also|[https://perldoc.perl.org/functions/formline formline(en)]|[https://perldoc.jp/func/formline formline(ja)]}} ==== lock ==== get a thread lock on a variable, subroutine, or method {{See also|[https://perldoc.perl.org/functions/lock lock(en)]|[https://perldoc.jp/func/lock lock(ja)]}} ==== prototype ==== get the prototype (if any) of a subroutine {{See also|[https://perldoc.perl.org/functions/prototype prototype(en)]|[https://perldoc.jp/func/prototype prototype(ja)]}} ==== reset ==== clear all variables of a given name {{See also|[https://perldoc.perl.org/functions/reset reset(en)]|[https://perldoc.jp/func/reset reset(ja)]}} ==== scalar ==== force a scalar context {{See also|[https://perldoc.perl.org/functions/scalar scalar(en)]|[https://perldoc.jp/func/scalar scalar(ja)]}} ==== undef ==== remove a variable or function definition {{See also|[https://perldoc.perl.org/functions/undef undef(en)]|[https://perldoc.jp/func/undef undef(ja)]}} === Modules === [[#do|do]] [[#import|import]] [[#no|no]] [[#package|package]] [[#require|require]] [[#use|use]] ==== do ==== turn a BLOCK into a TERM {{See also|[https://perldoc.perl.org/functions/do do(en)]|[https://perldoc.jp/func/do do(ja)]}} ==== import ==== patch a module's namespace into your own {{See also|[https://perldoc.perl.org/functions/import import(en)]|[https://perldoc.jp/func/import import(ja)]}} ==== no ==== unimport some module symbols or semantics at compile time {{See also|[https://perldoc.perl.org/functions/no no(en)]|[https://perldoc.jp/func/no no(ja)]}} ==== package ==== declare a separate global namespace {{See also|[https://perldoc.perl.org/functions/package package(en)]|[https://perldoc.jp/func/package package(ja)]}} ==== require ==== load in external functions from a library at runtime {{See also|[https://perldoc.perl.org/functions/require require(en)]|[https://perldoc.jp/func/require require(ja)]}} ==== use ==== load in a module at compile time and import its namespace {{See also|[https://perldoc.perl.org/functions/use use(en)]|[https://perldoc.jp/func/use use(ja)]}} === Namespace === [[#caller|caller]] [[#import|import]] [[#local|local]] [[#my|my]] [[#our|our]] [[#package|package]] [[#state|state]] [[#use|use]] ==== caller ==== get context of the current subroutine call {{See also|[https://perldoc.perl.org/functions/caller caller(en)]|[https://perldoc.jp/func/caller caller(ja)]}} ==== import ==== patch a module's namespace into your own {{See also|[https://perldoc.perl.org/functions/import import(en)]|[https://perldoc.jp/func/import import(ja)]}} ==== local ==== create a temporary value for a global variable (dynamic scoping) {{See also|[https://perldoc.perl.org/functions/local local(en)]|[https://perldoc.jp/func/local local(ja)]}} ==== my ==== declare and assign a local variable (lexical scoping) {{See also|[https://perldoc.perl.org/functions/my my(en)]|[https://perldoc.jp/func/my my(ja)]}} ==== our ==== declare and assign a package variable (lexical scoping) {{See also|[https://perldoc.perl.org/functions/our our(en)]|[https://perldoc.jp/func/our our(ja)]}} ==== package ==== declare a separate global namespace {{See also|[https://perldoc.perl.org/functions/package package(en)]|[https://perldoc.jp/func/package package(ja)]}} ==== state ==== declare and assign a persistent lexical variable {{See also|[https://perldoc.perl.org/functions/state state(en)]|[https://perldoc.jp/func/state state(ja)]}} ==== use ==== load in a module at compile time and import its namespace {{See also|[https://perldoc.perl.org/functions/use use(en)]|[https://perldoc.jp/func/use use(ja)]}} === Network === [[#endprotoent|endprotoent]] [[#endservent|endservent]] [[#gethostbyaddr|gethostbyaddr]] [[#gethostbyname|gethostbyname]] [[#gethostent|gethostent]] [[#getnetbyaddr|getnetbyaddr]] [[#getnetbyname|getnetbyname]] [[#getnetent|getnetent]] [[#getprotobyname|getprotobyname]] [[#getprotobynumber|getprotobynumber]] [[#getprotoent|getprotoent]] [[#getservbyname|getservbyname]] [[#getservbyport|getservbyport]] [[#getservent|getservent]] [[#sethostent|sethostent]] [[#setnetent|setnetent]] [[#setprotoent|setprotoent]] [[#setservent|setservent]] ==== endprotoent ==== be done using protocols file {{See also|[https://perldoc.perl.org/functions/endprotoent endprotoent(en)]|[https://perldoc.jp/func/endprotoent endprotoent(ja)]}} ==== endservent ==== be done using services file {{See also|[https://perldoc.perl.org/functions/endservent endservent(en)]|[https://perldoc.jp/func/endservent endservent(ja)]}} ==== gethostbyaddr ==== get host record given its address {{See also|[https://perldoc.perl.org/functions/gethostbyaddr gethostbyaddr(en)]|[https://perldoc.jp/func/gethostbyaddr gethostbyaddr(ja)]}} ==== gethostbyname ==== get host record given name {{See also|[https://perldoc.perl.org/functions/gethostbyname gethostbyname(en)]|[https://perldoc.jp/func/gethostbyname gethostbyname(ja)]}} ==== gethostent ==== get next hosts record {{See also|[https://perldoc.perl.org/functions/gethostent gethostent(en)]|[https://perldoc.jp/func/gethostent gethostent(ja)]}} ==== getnetbyaddr ==== get network record given its address {{See also|[https://perldoc.perl.org/functions/getnetbyaddr getnetbyaddr(en)]|[https://perldoc.jp/func/getnetbyaddr getnetbyaddr(ja)]}} ==== getnetbyname ==== get networks record given name {{See also|[https://perldoc.perl.org/functions/getnetbyname getnetbyname(en)]|[https://perldoc.jp/func/getnetbyname getnetbyname(ja)]}} ==== getnetent ==== get next networks record {{See also|[https://perldoc.perl.org/functions/getnetent getnetent(en)]|[https://perldoc.jp/func/getnetent getnetent(ja)]}} ==== getprotobyname ==== get protocol record given name {{See also|[https://perldoc.perl.org/functions/getprotobyname getprotobyname(en)]|[https://perldoc.jp/func/getprotobyname getprotobyname(ja)]}} ==== getprotobynumber ==== get protocol record numeric protocol {{See also|[https://perldoc.perl.org/functions/getprotobynumber getprotobynumber(en)]|[https://perldoc.jp/func/getprotobynumber getprotobynumber(ja)]}} ==== getprotoent ==== get next protocols record {{See also|[https://perldoc.perl.org/functions/getprotoent getprotoent(en)]|[https://perldoc.jp/func/getprotoent getprotoent(ja)]}} ==== getservbyname ==== get services record given its name {{See also|[https://perldoc.perl.org/functions/getservbyname getservbyname(en)]|[https://perldoc.jp/func/getservbyname getservbyname(ja)]}} ==== getservbyport ==== get services record given numeric port {{See also|[https://perldoc.perl.org/functions/getservbyport getservbyport(en)]|[https://perldoc.jp/func/getservbyport getservbyport(ja)]}} ==== getservent ==== get next services record {{See also|[https://perldoc.perl.org/functions/getservent getservent(en)]|[https://perldoc.jp/func/getservent getservent(ja)]}} ==== sethostent ==== prepare hosts file for use {{See also|[https://perldoc.perl.org/functions/sethostent sethostent(en)]|[https://perldoc.jp/func/sethostent sethostent(ja)]}} ==== setnetent ==== prepare networks file for use {{See also|[https://perldoc.perl.org/functions/setnetent setnetent(en)]|[https://perldoc.jp/func/setnetent setnetent(ja)]}} ==== setprotoent ==== prepare protocols file for use {{See also|[https://perldoc.perl.org/functions/setprotoent setprotoent(en)]|[https://perldoc.jp/func/setprotoent setprotoent(ja)]}} ==== setservent ==== prepare services file for use {{See also|[https://perldoc.perl.org/functions/setservent setservent(en)]|[https://perldoc.jp/func/setservent setservent(ja)]}} === Objects === [[#bless|bless]] [[#dbmclose|dbmclose]] [[#dbmopen|dbmopen]] [[#package|package]] [[#ref|ref]] [[#tie|tie]] [[#tied|tied]] [[#untie|untie]] [[#use|use]] ==== bless ==== create an object {{See also|[https://perldoc.perl.org/functions/bless bless(en)]|[https://perldoc.jp/func/bless bless(ja)]}} ==== dbmclose ==== breaks binding on a tied dbm file {{See also|[https://perldoc.perl.org/functions/dbmclose dbmclose(en)]|[https://perldoc.jp/func/dbmclose dbmclose(ja)]}} ==== dbmopen ==== create binding on a tied dbm file {{See also|[https://perldoc.perl.org/functions/dbmopen dbmopen(en)]|[https://perldoc.jp/func/dbmopen dbmopen(ja)]}} ==== package ==== declare a separate global namespace {{See also|[https://perldoc.perl.org/functions/package package(en)]|[https://perldoc.jp/func/package package(ja)]}} ==== ref ==== find out the type of thing being referenced {{See also|[https://perldoc.perl.org/functions/ref ref(en)]|[https://perldoc.jp/func/ref ref(ja)]}} ==== tie ==== bind a variable to an object class {{See also|[https://perldoc.perl.org/functions/tie tie(en)]|[https://perldoc.jp/func/tie tie(ja)]}} ==== tied ==== get a reference to the object underlying a tied variable {{See also|[https://perldoc.perl.org/functions/tied tied(en)]|[https://perldoc.jp/func/tied tied(ja)]}} ==== untie ==== break a tie binding to a variable {{See also|[https://perldoc.perl.org/functions/untie untie(en)]|[https://perldoc.jp/func/untie untie(ja)]}} ==== use ==== load in a module at compile time and import its namespace {{See also|[https://perldoc.perl.org/functions/use use(en)]|[https://perldoc.jp/func/use use(ja)]}} === Process === [[#alarm|alarm]] [[#exec|exec]] [[#fork|fork]] [[#getpgrp|getpgrp]] [[#getppid|getppid]] [[#getpriority|getpriority]] [[#kill|kill]] [[#pipe|pipe]] [[#qx/STRING/|qx/STRING/]] [[#readpipe|readpipe]] [[#setpgrp|setpgrp]] [[#setpriority|setpriority]] [[#sleep|sleep]] [[#system|system]] [[#times|times]] [[#wait|wait]] [[#waitpid|waitpid]] ==== alarm ==== schedule a SIGALRM {{See also|[https://perldoc.perl.org/functions/alarm alarm(en)]|[https://perldoc.jp/func/alarm alarm(ja)]}} ==== exec ==== abandon this program to run another {{See also|[https://perldoc.perl.org/functions/exec exec(en)]|[https://perldoc.jp/func/exec exec(ja)]}} ==== fork ==== create a new process just like this one {{See also|[https://perldoc.perl.org/functions/fork fork(en)]|[https://perldoc.jp/func/fork fork(ja)]}} ==== getpgrp ==== get process group {{See also|[https://perldoc.perl.org/functions/getpgrp getpgrp(en)]|[https://perldoc.jp/func/getpgrp getpgrp(ja)]}} ==== getppid ==== get parent process ID {{See also|[https://perldoc.perl.org/functions/getppid getppid(en)]|[https://perldoc.jp/func/getppid getppid(ja)]}} ==== getpriority ==== get current nice value {{See also|[https://perldoc.perl.org/functions/getpriority getpriority(en)]|[https://perldoc.jp/func/getpriority getpriority(ja)]}} ==== kill ==== send a signal to a process or process group {{See also|[https://perldoc.perl.org/functions/kill kill(en)]|[https://perldoc.jp/func/kill kill(ja)]}} ==== pipe ==== open a pair of connected filehandles {{See also|[https://perldoc.perl.org/functions/pipe pipe(en)]|[https://perldoc.jp/func/pipe pipe(ja)]}} ==== qx/STRING/ ==== backquote quote a string {{See also|[https://perldoc.perl.org/functions/qx/STRING/ qx/STRING/(en)]|[https://perldoc.jp/func/qx/STRING/ qx/STRING/(ja)]}} ==== readpipe ==== execute a system command and collect standard output {{See also|[https://perldoc.perl.org/functions/readpipe readpipe(en)]|[https://perldoc.jp/func/readpipe readpipe(ja)]}} ==== setpgrp ==== set the process group of a process {{See also|[https://perldoc.perl.org/functions/setpgrp setpgrp(en)]|[https://perldoc.jp/func/setpgrp setpgrp(ja)]}} ==== setpriority ==== set a process's nice value {{See also|[https://perldoc.perl.org/functions/setpriority setpriority(en)]|[https://perldoc.jp/func/setpriority setpriority(ja)]}} ==== sleep ==== block for some number of seconds {{See also|[https://perldoc.perl.org/functions/sleep sleep(en)]|[https://perldoc.jp/func/sleep sleep(ja)]}} ==== system ==== run a separate program {{See also|[https://perldoc.perl.org/functions/system system(en)]|[https://perldoc.jp/func/system system(ja)]}} ==== times ==== return elapsed time for self and child processes {{See also|[https://perldoc.perl.org/functions/times times(en)]|[https://perldoc.jp/func/times times(ja)]}} ==== wait ==== wait for any child process to die {{See also|[https://perldoc.perl.org/functions/wait wait(en)]|[https://perldoc.jp/func/wait wait(ja)]}} ==== waitpid ==== wait for a particular child process to die {{See also|[https://perldoc.perl.org/functions/waitpid waitpid(en)]|[https://perldoc.jp/func/waitpid waitpid(ja)]}} === Regexp === [[#m//|m//]] [[#pos|pos]] [[#qr/STRING/|qr/STRING/]] [[#quotemeta|quotemeta]] [[#s///|s///]] [[#split|split]] [[#study|study]] ==== m// ==== match a string with a regular expression pattern {{See also|[https://perldoc.perl.org/functions/m// m//(en)]|[https://perldoc.jp/func/m// m//(ja)]}} ==== pos ==== find or set the offset for the last/next m//g search {{See also|[https://perldoc.perl.org/functions/pos pos(en)]|[https://perldoc.jp/func/pos pos(ja)]}} ==== qr/STRING/ ==== compile pattern {{See also|[https://perldoc.perl.org/functions/qr/STRING/ qr/STRING/(en)]|[https://perldoc.jp/func/qr/STRING/ qr/STRING/(ja)]}} ==== quotemeta ==== quote regular expression magic characters {{See also|[https://perldoc.perl.org/functions/quotemeta quotemeta(en)]|[https://perldoc.jp/func/quotemeta quotemeta(ja)]}} ==== s/// ==== replace a pattern with a string {{See also|[https://perldoc.perl.org/functions/s/// s///(en)]|[https://perldoc.jp/func/s/// s///(ja)]}} ==== split ==== split up a string using a regexp delimiter {{See also|[https://perldoc.perl.org/functions/split split(en)]|[https://perldoc.jp/func/split split(ja)]}} ==== study ==== no-op, formerly optimized input data for repeated searches {{See also|[https://perldoc.perl.org/functions/study study(en)]|[https://perldoc.jp/func/study study(ja)]}} === Socket === [[#accept|accept]] [[#bind|bind]] [[#connect|connect]] [[#getpeername|getpeername]] [[#getsockname|getsockname]] [[#getsockopt|getsockopt]] [[#listen|listen]] [[#recv|recv]] [[#send|send]] [[#setsockopt|setsockopt]] [[#shutdown|shutdown]] [[#socket|socket]] [[#socketpair|socketpair]] ==== accept ==== accept an incoming socket connect {{See also|[https://perldoc.perl.org/functions/accept accept(en)]|[https://perldoc.jp/func/accept accept(ja)]}} ==== bind ==== binds an address to a socket {{See also|[https://perldoc.perl.org/functions/bind bind(en)]|[https://perldoc.jp/func/bind bind(ja)]}} ==== connect ==== connect to a remote socket {{See also|[https://perldoc.perl.org/functions/connect connect(en)]|[https://perldoc.jp/func/connect connect(ja)]}} ==== getpeername ==== find the other end of a socket connection {{See also|[https://perldoc.perl.org/functions/getpeername getpeername(en)]|[https://perldoc.jp/func/getpeername getpeername(ja)]}} ==== getsockname ==== retrieve the sockaddr for a given socket {{See also|[https://perldoc.perl.org/functions/getsockname getsockname(en)]|[https://perldoc.jp/func/getsockname getsockname(ja)]}} ==== getsockopt ==== get socket options on a given socket {{See also|[https://perldoc.perl.org/functions/getsockopt getsockopt(en)]|[https://perldoc.jp/func/getsockopt getsockopt(ja)]}} ==== listen ==== register your socket as a server {{See also|[https://perldoc.perl.org/functions/listen listen(en)]|[https://perldoc.jp/func/listen listen(ja)]}} ==== recv ==== receive a message over a Socket {{See also|[https://perldoc.perl.org/functions/recv recv(en)]|[https://perldoc.jp/func/recv recv(ja)]}} ==== send ==== send a message over a socket {{See also|[https://perldoc.perl.org/functions/send send(en)]|[https://perldoc.jp/func/send send(ja)]}} ==== setsockopt ==== set some socket options {{See also|[https://perldoc.perl.org/functions/setsockopt setsockopt(en)]|[https://perldoc.jp/func/setsockopt setsockopt(ja)]}} ==== shutdown ==== close down just half of a socket connection {{See also|[https://perldoc.perl.org/functions/shutdown shutdown(en)]|[https://perldoc.jp/func/shutdown shutdown(ja)]}} ==== socket ==== create a socket {{See also|[https://perldoc.perl.org/functions/socket socket(en)]|[https://perldoc.jp/func/socket socket(ja)]}} ==== socketpair ==== create a pair of sockets {{See also|[https://perldoc.perl.org/functions/socketpair socketpair(en)]|[https://perldoc.jp/func/socketpair socketpair(ja)]}} === String === [[#chomp|chomp]] [[#chop|chop]] [[#chr|chr]] [[#crypt|crypt]] [[#fc|fc]] [[#hex|hex]] [[#index|index]] [[#lc|lc]] [[#lcfirst|lcfirst]] [[#length|length]] [[#oct|oct]] [[#ord|ord]] [[#pack|pack]] [[#q/STRING/|q/STRING/]] [[#qq/STRING/|qq/STRING/]] [[#reverse|reverse]] [[#rindex|rindex]] [[#sprintf|sprintf]] [[#substr|substr]] [[#tr///|tr///]] [[#uc|uc]] [[#ucfirst|ucfirst]] [[#y///|y///]] ==== chomp ==== remove a trailing record separator from a string {{See also|[https://perldoc.perl.org/functions/chomp chomp(en)]|[https://perldoc.jp/func/chomp chomp(ja)]}} ==== chop ==== remove the last character from a string {{See also|[https://perldoc.perl.org/functions/chop chop(en)]|[https://perldoc.jp/func/chop chop(ja)]}} ==== chr ==== get character this number represents {{See also|[https://perldoc.perl.org/functions/chr chr(en)]|[https://perldoc.jp/func/chr chr(ja)]}} ==== crypt ==== one-way passwd-style encryption {{See also|[https://perldoc.perl.org/functions/crypt crypt(en)]|[https://perldoc.jp/func/crypt crypt(ja)]}} ==== fc ==== return casefolded version of a string {{See also|[https://perldoc.perl.org/functions/fc fc(en)]|[https://perldoc.jp/func/fc fc(ja)]}} ==== hex ==== convert a hexadecimal string to a number {{See also|[https://perldoc.perl.org/functions/hex hex(en)]|[https://perldoc.jp/func/hex hex(ja)]}} ==== index ==== find a substring within a string {{See also|[https://perldoc.perl.org/functions/index index(en)]|[https://perldoc.jp/func/index index(ja)]}} ==== lc ==== return lower-case version of a string {{See also|[https://perldoc.perl.org/functions/lc lc(en)]|[https://perldoc.jp/func/lc lc(ja)]}} ==== lcfirst ==== return a string with just the next letter in lower case {{See also|[https://perldoc.perl.org/functions/lcfirst lcfirst(en)]|[https://perldoc.jp/func/lcfirst lcfirst(ja)]}} ==== length ==== return the number of characters in a string {{See also|[https://perldoc.perl.org/functions/length length(en)]|[https://perldoc.jp/func/length length(ja)]}} ==== oct ==== convert a string to an octal number {{See also|[https://perldoc.perl.org/functions/oct oct(en)]|[https://perldoc.jp/func/oct oct(ja)]}} ==== ord ==== find a character's numeric representation {{See also|[https://perldoc.perl.org/functions/ord ord(en)]|[https://perldoc.jp/func/ord ord(ja)]}} ==== pack ==== convert a list into a binary representation {{See also|[https://perldoc.perl.org/functions/pack pack(en)]|[https://perldoc.jp/func/pack pack(ja)]}} ==== q/STRING/ ==== singly quote a string {{See also|[https://perldoc.perl.org/functions/q/STRING/ q/STRING/(en)]|[https://perldoc.jp/func/q/STRING/ q/STRING/(ja)]}} ==== qq/STRING/ ==== doubly quote a string {{See also|[https://perldoc.perl.org/functions/qq/STRING/ qq/STRING/(en)]|[https://perldoc.jp/func/qq/STRING/ qq/STRING/(ja)]}} ==== reverse ==== flip a string or a list {{See also|[https://perldoc.perl.org/functions/reverse reverse(en)]|[https://perldoc.jp/func/reverse reverse(ja)]}} ==== rindex ==== right-to-left substring search {{See also|[https://perldoc.perl.org/functions/rindex rindex(en)]|[https://perldoc.jp/func/rindex rindex(ja)]}} ==== sprintf ==== formatted print into a string {{See also|[https://perldoc.perl.org/functions/sprintf sprintf(en)]|[https://perldoc.jp/func/sprintf sprintf(ja)]}} ==== substr ==== get or alter a portion of a string {{See also|[https://perldoc.perl.org/functions/substr substr(en)]|[https://perldoc.jp/func/substr substr(ja)]}} ==== tr/// ==== transliterate a string {{See also|[https://perldoc.perl.org/functions/tr/// tr///(en)]|[https://perldoc.jp/func/tr/// tr///(ja)]}} ==== uc ==== return upper-case version of a string {{See also|[https://perldoc.perl.org/functions/uc uc(en)]|[https://perldoc.jp/func/uc uc(ja)]}} ==== ucfirst ==== return a string with just the next letter in upper case {{See also|[https://perldoc.perl.org/functions/ucfirst ucfirst(en)]|[https://perldoc.jp/func/ucfirst ucfirst(ja)]}} ==== y/// ==== transliterate a string {{See also|[https://perldoc.perl.org/functions/y/// y///(en)]|[https://perldoc.jp/func/y/// y///(ja)]}} === SysV === [[#msgctl|msgctl]] [[#msgget|msgget]] [[#msgrcv|msgrcv]] [[#msgsnd|msgsnd]] [[#semctl|semctl]] [[#semget|semget]] [[#semop|semop]] [[#shmctl|shmctl]] [[#shmget|shmget]] [[#shmread|shmread]] [[#shmwrite|shmwrite]] ==== msgctl ==== SysV IPC message control operations {{See also|[https://perldoc.perl.org/functions/msgctl msgctl(en)]|[https://perldoc.jp/func/msgctl msgctl(ja)]}} ==== msgget ==== get SysV IPC message queue {{See also|[https://perldoc.perl.org/functions/msgget msgget(en)]|[https://perldoc.jp/func/msgget msgget(ja)]}} ==== msgrcv ==== receive a SysV IPC message from a message queue {{See also|[https://perldoc.perl.org/functions/msgrcv msgrcv(en)]|[https://perldoc.jp/func/msgrcv msgrcv(ja)]}} ==== msgsnd ==== send a SysV IPC message to a message queue {{See also|[https://perldoc.perl.org/functions/msgsnd msgsnd(en)]|[https://perldoc.jp/func/msgsnd msgsnd(ja)]}} ==== semctl ==== SysV semaphore control operations {{See also|[https://perldoc.perl.org/functions/semctl semctl(en)]|[https://perldoc.jp/func/semctl semctl(ja)]}} ==== semget ==== get set of SysV semaphores {{See also|[https://perldoc.perl.org/functions/semget semget(en)]|[https://perldoc.jp/func/semget semget(ja)]}} ==== semop ==== SysV semaphore operations {{See also|[https://perldoc.perl.org/functions/semop semop(en)]|[https://perldoc.jp/func/semop semop(ja)]}} ==== shmctl ==== SysV shared memory operations {{See also|[https://perldoc.perl.org/functions/shmctl shmctl(en)]|[https://perldoc.jp/func/shmctl shmctl(ja)]}} ==== shmget ==== get SysV shared memory segment identifier {{See also|[https://perldoc.perl.org/functions/shmget shmget(en)]|[https://perldoc.jp/func/shmget shmget(ja)]}} ==== shmread ==== read SysV shared memory {{See also|[https://perldoc.perl.org/functions/shmread shmread(en)]|[https://perldoc.jp/func/shmread shmread(ja)]}} ==== shmwrite ==== write SysV shared memory {{See also|[https://perldoc.perl.org/functions/shmwrite shmwrite(en)]|[https://perldoc.jp/func/shmwrite shmwrite(ja)]}} === Time === [[#gmtime|gmtime]] [[#localtime|localtime]] [[#time|time]] [[#times|times]] ==== gmtime ==== convert UNIX time into record or string using Greenwich time {{See also|[https://perldoc.perl.org/functions/gmtime gmtime(en)]|[https://perldoc.jp/func/gmtime gmtime(ja)]}} ==== localtime ==== convert UNIX time into record or string using local time {{See also|[https://perldoc.perl.org/functions/localtime localtime(en)]|[https://perldoc.jp/func/localtime localtime(ja)]}} ==== time ==== return number of seconds since 1970 {{See also|[https://perldoc.perl.org/functions/time time(en)]|[https://perldoc.jp/func/time time(ja)]}} ==== times ==== return elapsed time for self and child processes {{See also|[https://perldoc.perl.org/functions/times times(en)]|[https://perldoc.jp/func/times times(ja)]}} === User === [[#endgrent|endgrent]] [[#endhostent|endhostent]] [[#endnetent|endnetent]] [[#endpwent|endpwent]] [[#getgrent|getgrent]] [[#getgrgid|getgrgid]] [[#getgrnam|getgrnam]] [[#getlogin|getlogin]] [[#getpwent|getpwent]] [[#getpwnam|getpwnam]] [[#getpwuid|getpwuid]] [[#setgrent|setgrent]] [[#setpwent|setpwent]] ==== endgrent ==== be done using group file {{See also|[https://perldoc.perl.org/functions/endgrent endgrent(en)]|[https://perldoc.jp/func/endgrent endgrent(ja)]}} ==== endhostent ==== be done using hosts file {{See also|[https://perldoc.perl.org/functions/endhostent endhostent(en)]|[https://perldoc.jp/func/endhostent endhostent(ja)]}} ==== endnetent ==== be done using networks file {{See also|[https://perldoc.perl.org/functions/endnetent endnetent(en)]|[https://perldoc.jp/func/endnetent endnetent(ja)]}} ==== endpwent ==== be done using passwd file {{See also|[https://perldoc.perl.org/functions/endpwent endpwent(en)]|[https://perldoc.jp/func/endpwent endpwent(ja)]}} ==== getgrent ==== get next group record {{See also|[https://perldoc.perl.org/functions/getgrent getgrent(en)]|[https://perldoc.jp/func/getgrent getgrent(ja)]}} ==== getgrgid ==== get group record given group user ID {{See also|[https://perldoc.perl.org/functions/getgrgid getgrgid(en)]|[https://perldoc.jp/func/getgrgid getgrgid(ja)]}} ==== getgrnam ==== get group record given group name {{See also|[https://perldoc.perl.org/functions/getgrnam getgrnam(en)]|[https://perldoc.jp/func/getgrnam getgrnam(ja)]}} ==== getlogin ==== return who logged in at this tty {{See also|[https://perldoc.perl.org/functions/getlogin getlogin(en)]|[https://perldoc.jp/func/getlogin getlogin(ja)]}} ==== getpwent ==== get next passwd record {{See also|[https://perldoc.perl.org/functions/getpwent getpwent(en)]|[https://perldoc.jp/func/getpwent getpwent(ja)]}} ==== getpwnam ==== get passwd record given user login name {{See also|[https://perldoc.perl.org/functions/getpwnam getpwnam(en)]|[https://perldoc.jp/func/getpwnam getpwnam(ja)]}} ==== getpwuid ==== get passwd record given user ID {{See also|[https://perldoc.perl.org/functions/getpwuid getpwuid(en)]|[https://perldoc.jp/func/getpwuid getpwuid(ja)]}} ==== setgrent ==== prepare group file for use {{See also|[https://perldoc.perl.org/functions/setgrent setgrent(en)]|[https://perldoc.jp/func/setgrent setgrent(ja)]}} ==== setpwent ==== prepare passwd file for use {{See also|[https://perldoc.perl.org/functions/setpwent setpwent(en)]|[https://perldoc.jp/func/setpwent setpwent(ja)]}} <noinclude> {{Nav}} {{DEFAULTSORT:Perl かんすう}} [[Category:Perl|かんすう]] </noinclude>
このページで使用されているテンプレート:
テンプレート:Main
(
ソースを閲覧
)
テンプレート:Nav
(
ソースを閲覧
)
テンプレート:Pathnav
(
ソースを閲覧
)
テンプレート:See also
(
ソースを閲覧
)
Perl/関数
に戻る。
ナビゲーション メニュー
個人用ツール
ログイン
名前空間
ページ
議論
日本語
表示
閲覧
ソースを閲覧
履歴表示
その他
検索
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
特別ページ
ツール
リンク元
関連ページの更新状況
ページ情報