2013-05-30 18 views
0

我在使用本地時對範圍進行了大量搜索。

我從這裏瞭解到:http://www.perlmonks.org/?node_id=94007它會在執行「本地」行和下一個塊結束時臨時更改變量的值。但是,它似乎也「滾動」perl閱讀光標?它似乎保留了以前的$ _。

這些是我的Perl腳本要求:

在中,讀取文本文件性病。 如果當前行匹配==,則將該行上的字符串打印爲具有相應格式的對應格式(每個==,===,====各不相同) 如果它包含!,則打印該行之前的部分!作爲屬性名稱和後面的部分!直到下一個!作爲價值(可能跨越多行)。

下面是一個示例文件:

$ cat dummy_spec.txt 
== title1 (level 1) == 
=== title2 (level 2) === 
title2p2 ! 
2p2_1 
! 
title2p2 ! Line 1 
! 
title2p1 ! 
echo "$tam ta" 
! 
title2p3 ! 
2p3 
! 
title2p2 ! Li 
ne split 
! 
==== title3 (level 3) ==== 
title3p1 ! 
one 
two 
three 
! 

這裏是我的代碼:

#!/usr/bin/env perl 
my $propName=''; 
my $propVal=''; 
while (<>){ 
    #print "new loop number $., contents $_ \n"; 
    $propName=''; 
    $propVal=''; 

    my $str=$_; 
    chomp $str; 

    if ($str=~/==\s+.+\s+==/)#If line is a Category, print category 
    { 
     $str=~s/=//g; 
     print "\t\t\t<tr class=\"category\"><td colspan=\"2\">$str</td></tr>\n"; 
    } elsif($str=~/===\s+.+\s+===/)#If line is a list, print list title 
    { 
     $str=~s/=//g; 
     print "\t\t\t<tr class=\"list\"><td colspan=\"2\">$str</td></tr>\n"; 
    } elsif($str=~/====\s+.+\s+====/)#If line is an item, print item title 
    { 
     $str=~s/=//g; 
     print "\t\t\t<tr class=\"item\"><td>$str</td></tr>\n"; 
    } elsif($str=~/!/)#If line is a property, print name and value 
    { 
     ($propName,$propVal)=split '!', $_; 
     chop $propVal; 
     print "This is a property ($.) with name : $propName and first line : $propVal \n"; 
     local $/ = "!\n"; 
     if(<>){#custom $/ should still be in scope here.    
      chomp; 
      $propVal="$propVal -~- $_"; 
      print "This is a property ($.) with name : $propName and value : $propVal \n"; 
     } 
    } 

} 

輸出是不是應該是什麼:

$ cat dummy_spec.txt |./multi 
      <tr class="category"><td colspan="2"> title1 (level 1) </td></tr> 
      <tr class="category"><td colspan="2"> title2 (level 2) </td></tr> 
This is a property (3) with name : title2p2 and first line : 
This is a property (4) with name : title2p2 and value : -~- title2p2 
This is a property (5) with name : title2p2 and first line : Line 1 
This is a property (6) with name : title2p2 and value : Line 1 -~- title2p2 ! Line 1 

This is a property (7) with name : title2p1 and first line : 
This is a property (8) with name : title2p1 and value : -~- title2p1 ! 

This is a property (9) with name : title2p3 and first line : 
This is a property (10) with name : title2p3 and value : -~- title2p3 
This is a property (11) with name : title2p2 and first line : Li 
This is a property (12) with name : title2p2 and value : Li -~- title2p2 ! Li 

      <tr class="category"><td colspan="2"> title3 (level 3) </td></tr> 
This is a property (14) with name : title3p1 and first line : 
This is a property (15) with name : title3p1 and value : -~- title3p1 

它似乎還「滾回」perl閱讀光標?它打印前面的$ _。

我也很抱歉,因爲我的Perl是非常基本的。

回答

3

你不是在用範圍的$/您更改值分配給$_,只有 從輸入讀取並使用所讀取的行爲,如果 語句的條件。

從文件中讀取時的神奇賦值給變量$_僅 發生,如果<>運營商是一個while 循環狀況的唯一內容,它並不適用於if語句的條件。請參閱documentation for I/O Operators中的第四個 段落。

你可能想改變這種狀況,如果語句:

if(defined ($_ = <>)) 
+0

非常感謝!解決了它。有時候Perl太微妙了。 – xor007

相關問題