関数順 インデックス
目的別 インデックス

構文
 
 scalar 評価対象

返り値
 
 指定した値をスカラー・コンテキストで評価した結果

説明

  • 指定した値ををスカラー・コンテキストで評価します。例えば,printのようにリストを引数に取る関数にlocaltimeのようなリスト・コンテキストとスカラー・コンテキストで結果が変わる関数を指定すると,リスト・コンテキストに対する値を返します。このときscalarを使うと,スカラー・コンテキストに対する値を返すことができます。

使用例

localtimeの結果をスカラー・コンテキストとリスト・コンテキストの両方で出力する
#!/usr/bin/perl
use strict;
use warnings;

print scalar(localtime), "\n"; 
# スカラーコンテキストで評価

print "\n";

print localtime, "\n"; 
# リストコンテキストで評価

print "\n";
配列@itemsをスカラー・コンテキストとリスト・コンテキストの両方で出力する
#!/usr/bin/perl
use strict;
use warnings;

my @items = (1, 2, 3);

print scalar(@items), "\n"; 

print "\n";

print @items, "\n"; 
print "\n";