perl檔案包含(do,require,use)指令介紹_perl

來源:互聯網
上載者:User

1. do:

1)形式:
do 'filename';
說明:
這裡filename需要添加單引號,否則會出錯;
filename可以為任何尾碼的,甚至沒有尾碼,不要求是pl或者pm等。

2)關於do的理解:
do 'filename'
首先需要讀入filename的檔案(如果讀入失敗,返回undef而且會設定$!變數);
如果讀入成功,然後對filename讀入的語句進行編譯(如果無法編譯或者編譯錯誤,會返回undef而且設定錯誤資訊到$@變數);
如果編譯也成功,do會執行filename中的語句,最終返回最有一個運算式的值。

簡短表達do 'filename'的功能,就是能夠將filename中的文字全部載入到當前檔案中。

3)理解do的用法:
a. 將檔案拆分:
main.pl:

複製代碼 代碼如下:

use strict;
do 'seperate'; #檔案可以以任何尾碼命名甚至沒有尾碼;
seperate:
print "Hello from seperate file! :-)";

b. 可以在seperate中定義函數,然後在當前檔案中調用:
main.pl

複製代碼 代碼如下:

#!/usr/bin/perl
use strict;
do 'seperate';
show();
seperate:
sub show{
    print "Hello from seperate file! :-)";
}

c. 可以在seperate中定義package,然後在當前檔案中調用:
main.pl

複製代碼 代碼如下:

#!/usr/bin/perl
use strict;
do 'seperate';
Show::show_sentence();
seperate:
package Show;
sub show_sentence(){
    print "Hello from seperate file! :-)";
}
1;
__END__
#都不需要文

件名必須與package的名稱相同,而且seperate都不需要pm尾碼。

從上面的例子,很容易得到,使用do可以方便地實現檔案包含。
更多參看http://perldoc.perl.org/functions/do.html

2. require
參看http://perldoc.perl.org/functions/require.html
1)形式:
require 'filename';
require "filename";
這兩種相同,而且和do的使用方法都類似;

require Module;
如果不加單引號或者雙引號的話,後面的Module將被解析為Perl的模組即.pm檔案,然後根據@INC Array中搜尋Module.pm檔案。首先在目前的目錄下搜尋Module.pm的檔案(使用者自訂的),如果找不到再去Perl的 (@INC contains: C:/Perl/site/lib C:/Perl/lib .)尋找。
如果Module中出現::,如require Foo::Bar; 則會被替換為Foo/Bar.pm

2)關於require使用的解釋:
如果使用require 'filename'或者require "filename"來包含檔案的話,使用方法和do完全近似;
如果使用require Module的話,則需要定義Module.pm的檔案而且檔案中的package要以Module來命名該模組。
main.pl

複製代碼 代碼如下:

#!C:\perl\bin\inperl -w
use strict;
require Show;
Show::show_header();

Show.pm
#Show.pm

複製代碼 代碼如下:

package Show;
sub show_header(){
    print "This is the header!  ";   
    return 0;
}
sub show_footer(){
    print "This is the footer!  ";   
    return 0;
}
1;
__END__

3. use
參看http://perldoc.perl.org/functions/use.html
1)形式:
use Module;
use只能夠使用模組,而且和require的用法相似,需要Module.pm的檔案,而且檔案中的package需要已Module來命名該模組。

main.pl

複製代碼 代碼如下:

#!C:\perl\bin\perl -w
use strict;
use Show;
Show::show_header();

Show.pm

複製代碼 代碼如下:

#Show.pm
package Show;
sub show_header(){
    print "This is the header!  ";   
    return 0;
}
sub show_footer(){
    print "This is the footer!  ";   
    return 0;
}
1;
__END__

2)require和use的區別:
require:
do the things at run time; (運行時載入)
use:
do the things at compile time; (編譯時間載入)

4. perlmod - Perl modules (packages)
參考http://perldoc.perl.org/perlmod.html
1)  樣本:

複製代碼 代碼如下:

#Show.pm
package Show;
sub show_header(){
    print "This is the header! /n";  
    return 0;
}
sub show_footer(){
    print "This is the footer! /n";  
    return 0;
}
1;
__END__

2)
一般檔案名稱需要和package名稱相同,這裡為Show;
可以定義變數和函數;
不要忘記1;
以及最後的__END__

3)
在別的檔案中,使用require或者use使用模組的時候:

複製代碼 代碼如下:

use Show;
#require Show;
Show::show_header();

5. Perl的函數定義及調用:
 

複製代碼 代碼如下:

sub fun_name(){
 #...
}

1) 如果定義在使用之前,在使用的時候直接fun_name();即可
2)如果定義在使用之後,之前使用的時候需要使用&fun_name();來調用函數。

6.
小結:
綜上,檔案包含可以提高代碼的複用性,在Perl是實現檔案包含可以才去兩條路:
1)使用do或者require(帶引號的)那種方式;
2)使用require Module或者use Module的模組方式;
兩者均可。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.