2013年3月6日 星期三

perl 判斷整數

http://bbs.chinaunix.net/thread-1596012-1-1.html

使用正規表示式:

$ perl -e 'my $xx=1 ;if ($xx=~/^-?\d+$/){warn "int";}else{warn "not int"};warn $xx'

判斷資料型態(數字、字串、refrence-hash、reference-array):
use Scalar::Util qw(looks_like_number);

my $val;
$val = { a => "123", b => "456" };
$val = [{ a => "123", b => "456" }];
$val = 6486;
$val = "string test"

if(ref($val)){
  print ref($val);
}else{
  print "is", looks_like_number($val) ? '' : ' not', " a number\n";
}

http://stackoverflow.com/questions/1731333/how-do-i-tell-what-type-of-value-is-in-a-perl-variable
使用ref()
http://stackoverflow.com/questions/12647/how-do-i-tell-if-a-variable-has-a-numeric-value-in-perl
使用 use Scalar::Util qw(looks_like_number);