![](/img/trans.png)
[英]Can't use string as a HASH ref error when using perl JSON module to decode
[英]Error in conditionally including Perl module using if pragma
這很好用:
use if (1), 'x86_64-linux-thread-multi::Devel::Cover::DB::IO::JSON';
但不是這個:
my $a=1;
use if ($a), 'x86_64-linux-thread-multi::Devel::Cover::DB::IO::JSON';
稍后打印錯誤Can't locate object method "new" via package "JSON" (perhaps you forgot to load "JSON"?) at ./script.pl line 100.
我有$json = JSON->new;
在第100行中。
我正在使用Perl 5.16.2版本,有什么幫助嗎? 提前致謝。
變量的分配發生在運行時,而use
發生在編譯時。 因此,在你的榜樣, $a
尚未分配的任何值時use
情況發生,所以它的計算結果為undef
。
要在編譯時分配給$a
,請使用BEGIN
:
my $a;
BEGIN { $a = 1 }
use if ($a), 'x86_64-linux-thread-multi::Devel::Cover::DB::IO::JSON';
您還應該知道,使變量$a
和$b
詞法通常是令人厭煩的,因為這會干擾sort
函數的正常操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.