如何解決以下問題?在perl中區分字符串和數字參數
use 5.014;
use warnings;
use Test::Simple tests => 4;
ok(doit(0123) == 83, "arg as octal number");
ok(doit(83) == 83, "arg as decimal number");
ok(doit('0123') == 83, "arg as string with leading zero");
ok(doit('123') == 83, "arg as string without leading zero");
sub doit {
my $x = shift;
return $x; # how to replace this line
#return got_the_arg_as_string ? oct($x) : $x; # with something like this
}
例如,如果我傳遞doit
子任何字符串 - 意味着引用值 - (帶或不帶前導零),它應該被轉換爲八進制值。否則,它只是一個數字。
想知道行爲不同。對我來說,這看起來像是未來錯誤的來源... – jm666
@cajwine:這與您在想要doit('123')的問題中所說的內容相矛盾嗎? – ysth