構文 |
|
sprintf 書式文字列, 出力データ |
返り値 |
|
書式に従ってフォーマットした文字列 |
説明
- 指定した書式でフォーマットした文字列を返します。
- 書式文字列の中に記述するフィールド指定子はprintfの項を参照して下さい。
使用例
#!/usr/bin/perl
use strict;
use warnings;
my $dir = '/home/user1/data';
my $filename = 'sample.dat';
my $path = sprintf "%s/%s", $dir, $filename;
if (-f $path) {
print "$path found\n";
} else {
print "$path not found\n";
}
#!/usr/bin/perl
use strict;
use warnings;
my ($day, $month, $year) = (localtime)[3..5];
$year += 1900;
$month++;
my $today = sprintf "%04d-%02d-%02d", $year, $month, $day;
print "Today is $today.\n";