Scalar 変数の型を取得する
- ref:Kazuho@Cybozu Labs: DBI::Printf - A Yet Another Prepared Statement
- ref:404 Blog Not Found:perl - $scalarの中身が数値か否かを判定する
Perl は数値と文字列の区別が無いと思われがちだけど、それは Perl から区別する方法がないだけで、C のレベルではきちんと区別できたりする。
なので、XS で型を取得するモジュールとか書いてみた。
テストコードはこんなの。
use Test::More tests => 10; BEGIN { use_ok('Scalar::Type') }; my $a = 10; is(type($a), 'integer'); my $b = 10.1; is(type($b), 'float'); my $c = "10"; is(type($c), 'string'); my $d = "10.1"; is(type($d), 'string'); is(type([0, 1, 2]), 'arrayref'); is(type({a => 10, b => 10}), 'hashref'); is(type(sub { print 10; }), 'coderef'); open my $fh, '<', __FILE__ or die $!; is(type($fh), 'globref'); close $fh; is(type(\*ARGV), 'globref');
どうでせう。