<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 開啟?`strict`?和?`warnings` 無論何時調試代碼,都確信已開啟了?`strict`?和?`warnings`?編譯指令。 將下面兩行: ~~~ use strict; use warnings; ~~~ 放到你試圖調試或將來可能想調試的程序的頂部。 [strict](http://perldoc.perl.org/strict.html)?編譯指令強制你使用那些允許 Perl 在編譯時找出錯誤的許多特性。 首先最重要的是,在?`strict`?下,變量必須在使用前聲明。多數情況下,這意味 著使用?`my`: ~~~ use strict; my $foo = 7; # OK, normal variable print "foo is $fooo\n"; # Perl complains and aborts compilation ~~~ 沒有?`strict`,Perl 仍然會高興地執行上面的程序。但你可能會感到杯具,想不 明白`$foo`?為何沒有值。啟用?`strict`?也會減少許多令人頭痛的輸入錯誤。 另外,`strict`?不允許使用多數裸字。 ~~~ no strict; $foo = Lorem; print "$foo\n"; # Prints "Lorem" use strict; my $foo = ipsum; # Complains about bareword $foo = ( Lorem => 'ipsum' # OK, barewords allowed on left of => ); $SIG{PIPE} = handler; # Complains $SIG{PIPE} = \&handler; # OK $SIG{PIPE} = "handler"; # Also, OK, but above is preferred ~~~ 最后,如果你使用[符號引用](http://perldoc.perl.org/perlref.html#Symbolic-references),啟用?`strict`?將拋出運行時錯誤。 ~~~ no strict; $name = "foo"; $$name = "bar"; # Sets the variable $foo to 1 print "$name $$name\n"; # Prints "foo bar" use strict; my $name = "foo"; $$name = "bar"; # Complains: can't use "foo" as ref ~~~ `warnings`?編譯指令將使 Perl 吐出許多有用的抱怨,以便讓你知道程序中的 某個東東并非你所想要的: ~~~ use warnings; my $foo = ; $foo += 3; my $foo = 1; # Compains: redeclaration of variable my $bar = '12fred34'; my $baz = $bar + 1; # Complains: Argument "12fred34" isn't numeric # Complains: Name "main::baz" used only once ~~~ 參閱 strict 及 warnings 的文檔了解其他信息。關于不用?`strict`?所帶來的 恐怖故事,可以看看?[PerlMonks](http://www.perlmonks.org/?node_id=482733)?上面的帖子。 ## 檢查每個?`open`?的返回值 你將經常看到人們抱怨下面的代碼無法執行: ~~~ open( my $file, $filename ); while ( <$file> ) { ... } ~~~ 接著抱怨?`while`?循環也被破壞了。這兒的常見問題是文件?`$filename`?實際 上并不存在,因此?`open`?將失敗。如果沒有檢查,那么你將從來不會知道。使 用以下代碼替換它: ~~~ open( my $file, '<', $filename ) or die "Can't open $filename: $!"; ~~~ ## 利用?`diagnostics`?擴展警告 有時候警告消息并沒有解釋你想要的那么多。例如,為何你會獲得此警告? ~~~ Use of uninitialized value in string eq at /Library/Perl/5.8.6/WWW/Mechanize.pm line 695. ~~~ 試試將下列內容放到程序頂部并重新執行代碼: ~~~ use diagnostics; ~~~ 現在警告看起來像這樣: ~~~ Use of uninitialized value in string eq at /Library/Perl/5.8.6/WWW/Mechanize.pm line 695 (#1) (W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables. To help you figure out what was undefined, perl tells you what operation you used the undefined value in. Note, however, that perl optimizes your program and the operation displayed in the warning may not necessarily appear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo, and the warning will refer to the concatenation (.) operator, even though there is no . in your program. ~~~ 更多的信息將幫助你找出程序的問題。 記住你也可以從命令行指定模塊和編譯指令,因此你甚至不必編輯源代碼來使用`diagnostics`。使用?`-M`?命令行選項再次執行它: ~~~ perl -Mdiagnostics mycode.pl ~~~ ## 使用優化信號來獲得棧跟蹤信息 有時候你將獲得警告,但你并不明白是如何得到的。比如: ~~~ Use of uninitialized value in string eq at /Library/Perl/5.8.6/WWW/Mechanize.pm line 695. ~~~ 你可以看到模塊的 695 行代碼干了什么,但你無法看到你的代碼在此時干了什么。 你將需要看到子例程被調用的跟蹤信息。 當 Perl 調用?`die`?或?`warn`?時,它將分別指定?`$SIG{__DIE__}`?和?`$SIG{__WARN__}`?來通過子例程。如果你修改它們,讓其成為比?`CORE::die`?和?`CORE::warn`?更有用的話,你就得到了一個有用的調試工具。這種情況,可以使用?`Carp`?模塊的?`confess`?函數。 在你程序的頂部,添加這些行: ~~~ use Carp qw( confess ); $SIG{__DIE__} = \&confess; $SIG{__WARN__} = \&confess; ~~~ 現在,當代碼調用?`warn`?或?`die`?時,`Carp::confess`?函數將處理它。`confess`?打印原始警告,跟著棧跟蹤信息,然后停止執行程序。 ~~~ Use of uninitialized value in string eq at /Library/Perl/5.8.6/WWW/Mechanize.pm line 695. at /Library/Perl/5.8.6/WWW/Mechanize.pm line 695 WWW::Mechanize::find_link('WWW::Mechanize=HASH(0x180e5bc)', 'n', 'undef') called at foo.pl line 17 main::go_find_link('http://www.cnn.com') called at foo.pl line 8 ~~~ 現在我們有更多信息來調試代碼,包括精確的調用函數及傳遞的參數。從這兒, 我們能夠容易地看到?`find_link`?的第三個參數是?`undef`,那將是一個開始 調查的好地方。 ## 使用 Carp::Always 獲得棧跟蹤信息 如果你不想覆蓋信號處理器,那么可以安裝 CPAN 模塊?[Carp::Always](https://metacpan.org/release/Carp-Always)。 在安裝之后,添加下行到你的代碼中: ~~~ use Carp::Always; ~~~ 或者使用?`-MCarp::Always`?從命令行調用你的程序,這將總是會得到棧跟蹤信息。 ## 使用 Devel::REPL 交互執行 Perl 代碼 [Devel::REPL](https://metacpan.org/release/Devel-REPL)?模塊提供一個交互式的 Shell。通過該 Shell,你不用創建 臨時的源代碼文件就可以做快速的原型開發及測試代碼。 在安裝 Devel::REPL 之后,你可以執行以下命令啟動 Shell: ~~~ $ re.pl ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看