跳轉到內容

Perl 程式設計/關鍵字/require

來自華夏公益教科書,自由的教學讀物
前一個:rename 關鍵字 下一個:reset

Therequirekeyword

[編輯 | 編輯原始碼]

Therequire關鍵字要求 PerlVERSION, 一些語義由EXPRESSION指定,或者存在於$_中,如果EXPRESSION丟失。VERSION可以是類似於5.006的數字引數,這將與$]比較,或者是一個形式為v5.6.1的數字引數,這將與$^V (= $PERL_VERSION的文字。應避免使用文字形式,因為它會導致早期 Perl 版本中出現誤導性的錯誤資訊。如果VERSION大於 Perl 直譯器的版本,則會引發異常。

如果沒有這些引數,require要求包含一個檔案,前提是該檔案以前尚未包含。使用相同的指定名稱,該檔案不會包含兩次。包含的檔案必須在結尾返回 true,因此,習慣上使用1.

如果EXPRESSION是裸字,require假設這是一個.pm副檔名,將所有::替換為/,以便標準模組輕鬆載入。

在搜尋具有.pm副檔名的檔案之前,require首先搜尋具有.pmc副檔名的檔案,如果找到,則載入它而不是.pm檔案。

  require VERSION
  require EXPRESSION
  require
require v5.14.2;    # runtime version check
require 5.14.2;     # ditto
require 5.014_002;  # preferred for backwards compatibility

require Foo::Bar;   # a splendid bareword
# Will look for the "Foo/Bar.pm" file in the directories specified in the @INC array.

$class = 'Foo::Bar';
require $class;     # $class is not a bareword
require "Foo::Bar"; # not a bareword because of the ""
# Will look for the "Foo::Bar" file in the @INC array and will complain not finding it.
前一個:rename 關鍵字 下一個:reset
華夏公益教科書