2010-07-23 59 views
0

我只是想學習一些Perl,所以我正在通過一個函數來掌握語言。有人可以向我解釋這個函數究竟在做什麼?這個Perl函數有什麼作用?

#! /usr/bin/perl 
use strict; 

my %hash; 

&Parse('first.txt'); 
&Parse('second.txt'); 

my $outputpath = 'output.txt'; 
unlink ($outputpath); 
open (OUTPUT, ">>$outputpath") || die "Failed to open OUTPUT ($outputpath) - $!"; 
print OUTPUT "$_ \t" . join("\t", @{$hash{$_}}) . "\n" foreach (sort keys %hash); 
close (OUTPUT) || die "Failed to close OUTPUT ($outputpath) - $!"; 

sub Parse { 
    my $inputpath = shift; 
    open (INPUT, "<$inputpath") || die "Failed to open INPUT ($inputpath) - $!"; 
    while (<INPUT>) { 
     chomp; 
     my @row = split(/\t/, $_); 
     my $col1 = $row[0]; 
     shift @row; 
     push(@{$hash{$col1}}, @row); 
    } 
    close (INPUT) || die "Failed to close INPUT ($inputpath) - $!"; 
    return 1; 
} 

我更感興趣的是shiftpushchomp

+1

http://p3rl.org/shift http://p3rl.org/push http://p3rl.org/chomp – daxim 2010-07-23 08:55:04

+0

@Svante:避免無益的批示你上面發佈的那個。這對OP有什麼幫助?也許他找不到手冊,或找不到手冊中的哪個部分。 – Konerak 2010-07-23 13:49:44

+0

我想我可以從中發佈的那四個字母派生出來。 :) – Svante 2010-07-23 16:42:43

回答

4

編輯:您發佈了一些額外的代碼,我會說,以及評論。

#!/usr/bin/perl 
#The first line (has to be first, hence this comment comes after) allows the linux shell to know 
#this is a perl program, and to call perl to execute it. 

#use strict: allow stricter checking of perl syntax. You should always do this. 
use strict; 

#declare a global variable called hash - not a very good name... 
my %hash; 

#call the method with 'first.txt' as argument 
&Parse('first.txt'); 
&Parse('second.txt'); #same thing, different parameter 


my $outputpath = 'output.txt'; 

#destroy the file declared above if it exists 
unlink ($outputpath); 

# open the file for append (could simple have opened for output and not used the unlink above...) 
open (OUTPUT, ">>$outputpath") || die "Failed to open OUTPUT ($outputpath) - $!"; 

#print a line to output 
#the line comes from a foreach loop 
#the foreach loop runs over the hash, sorted by key 
#each hash entry contains an array, this array is converted by a string using the JOIN function 
# the join function will paste the elements of the array into a string, seperated by a tab 
print OUTPUT "$_ \t" . join("\t", @{$hash{$_}}) . "\n" foreach (sort keys %hash); 

#Close the outputfile 
close (OUTPUT) || die "Failed to close OUTPUT ($outputpath) - $!"; 

這個程序很可能是前段時間寫的 - 現代Perl看起來有點不同,並且有一些最佳實踐還沒有出現。

不要以此爲例來介紹如何編寫Perl。也許Ether將改寫這個給你,如果你的微笑很好:)

#declare a sub 
sub Parse { 
    # the parameters given to a sub are stored in @_. 
    #shift without arguments takes the first element from @_ 
    my $inputpath = shift; 
    #opens the file "inputpath" into fileglob INPUT. 
    #If this fails, quit with an error message 
    open (INPUT, "<$inputpath") || die "Failed to open INPUT ($inputpath) - $!"; 

    #loop over the file one line at the time, putting each line in $_ 
    while (<INPUT>) { 

     #chop = remove last character. chomp = remove last character if it is a CRLF. 
     #Without arguments, works on $_ 
     chomp; 

     #split the $_ variable (containing the row) 
     #into an array based on the tab character 
     my @row = split(/\t/, $_); 

     # take the first element into col1 
     my $col1 = $row[0]; 

     # shift it (remove the first element) 
     shift @row; 

     # actually both rows above can be just written as one statement: 
     my $col1 = shift @row; 

     #the $hash variable is probably a global hashref defined somewhere above... 
     #the $hash hashref now contains a bucket named with the 'col1' value 
     # the value of that bucket is the array of the row we just read 
     push(@{$hash{$col1}}, @row); 

     # end the while loop 
    } 

    #close the file or die 
    close (INPUT) || die "Failed to close INPUT ($inputpath) - $!"; 

    #end the method 
    return 1; 
} 
+0

這是我能希望得到的最好的解釋。謝謝 – Vijay 2010-07-23 09:49:43

+0

現在我已經粘貼了完整的程序。你還可以解釋一下打印語句嗎? – Vijay 2010-07-23 10:02:43

2

如果你有一個健全的Perl安裝下面的命令行命令將幫助:

perldoc -f shift 
perldoc -f push 
perldoc -f chomp 

您也一定會喜歡:

perldoc perlfunc 
perldoc perlvar 

千萬不要錯過perlvar關於$_的部分內容,否則您將永遠無法獲得perl的內容。

你會逐漸注意到perl不是面向對象的,它支持對象,但這是一個非常奇怪的實現。 Perl更多面向工作完成工作通常與提取或翻譯某種數據集有關。

Perl的一個襯墊是最強大的命令行,你永遠不會寫:

perl -pe 's/(\d*)/$1*10/ge' 

查覈在perldoc perlrun

-p-e-n-i開關(這是主要的原因Perl的一個6被預定爲主要改寫,直到現在它已經在之後始終在之後,並且定於當天發佈永遠的毀滅公爵

shift無論如何是像Python的some_array.pop(1)或JavaScript的some_array.shift()

push就像python的some_array.append(junk)或JavaScript的some_array.push(more_junk)

chomp確實很奇特,實際上是chop的跨平臺版本:它從已從stdin讀取的行中刪除行尾字符。這是一種攻克這個小鑽石運算符<>(檢查perldoc perlop - 「I/O操作符」部分)缺陷:鑽石讀取標準輸入或命令行文件參數逐行,但它不會刪除\n。 (也不是\r\n

chomp之後刪除它們。 (chop只刪除\n獨自離開\r。)

+0

警告的最後一句話:閱讀其他人的perl *(甚至在幾周之後)*可能是一種*皇家痛苦*,注重於故意的例子和手冊。 Perl文檔是我見過的更廣泛的文檔。 – ZJR 2010-07-23 09:36:38

+0

不,它沒有閱讀其他peoplr的Perl。它是由不懂如何進行軟件開發的人閱讀Perl,更具體地說是如何編寫可讀代碼。所有人都會用任何語言編寫不可讀的代碼。 – DVK 2010-07-23 11:22:15

+0

對不起,我一直在閱讀核心模塊:同行評審,縮排,評論良好,記錄和單元測試:但它仍然是一個痛苦。 – ZJR 2011-03-07 23:50:09