2016-02-22 20 views
0

此腳本從下載的網頁中剔除網址。我在使用這個腳本時遇到了一些麻煩 - 當我使用"my $csv_html_line = @_ ;" 然後打印出"@html_LineArray" - 它只打印出"1's"。當我用"my $csv_html_line = shift ;"替換 "my $csv_html_line = @_ ;"時,該腳本正常工作。 我不知道有什麼區別betweeh的"= @_" and shift - 監守我認爲 沒有指定的東西,在一個子程序,SHIFT SHIFT 320交織"@_".子例程中的Perl特殊變量「@_」無法正常工作

#!/usr/bin/perl 
use warnings; 
use strict ; 

sub find_url { 
    my $csv_html_line = @_ ; 
    #my $csv_html_line = shift ; 
    my @html_LineArray = split("," , $csv_html_line) ; 
    print "@html_LineArray\n" ; 
    #foreach my $split_line(@html_LineArray) { 
    # if ($split_line =~ m/"adUrl":"(http:.*)"/) { 
    #  my $url = $1; 
    #  $url =~ tr/\\//d; 
    #  print("$url\n") ; 
    # } 
    #} 
} 



my $local_file = "@ARGV" ; 
open(my $fh, '<', "$local_file") or die "cannot open up the $local_file $!" ; 
while(my $html_line = <$fh>) { 
    #print "$html_line\n"; 
    find_url($html_line) ; 
} 

這就是上面會打印出來。

1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 

這工作得很好 - 它使用換檔而不是「@_」

#!/usr/bin/perl 
use warnings; 
use strict ; 

sub find_url { 
    #my $csv_html_line = @_ ; 
    my $csv_html_line = shift ; 
    my @html_LineArray = split("," , $csv_html_line) ; 
    #print "@html_LineArray\n" ; 
    foreach my $split_line(@html_LineArray) { 
     if ($split_line =~ m/"adUrl":"(http:.*)"/) { 
      my $url = $1; 
      $url =~ tr/\\//d; 
      print("$url\n") ; 
     } 
    } 
} 



my $local_file = "@ARGV" ; 
open(my $fh, '<', "$local_file") or die "cannot open up the $local_file $!" ; 
while(my $html_line = <$fh>) { 
    #print "$html_line\n"; 
    find_url($html_line) ; 
} 
+0

http://stackoverflow.com/questions/2126365/whats-the -disference-between-my-variablename-and-my-variablename-in-perl – mob

回答

6

這是

my ($csv_html_line) = @_ ; 

你寫你在標量環境@_代碼,並獲得方式其長度(元素數量)。正如你提到的,

my $csv_html_line = shift; 

作品,因爲shift運營商需要一個列表,並移除並返回的第一個元素作爲標量。

+0

如果我在特殊變量周圍放置雙引號,my $ csv_html_line =「@_」; – capser

+4

你爲什麼要這麼做?你相信什麼完成?如果'@ _'包含多個字符串,則引用它將返回連接的所有成員字符串,以空格分隔。可能不是你想要的。 –

+0

引用特殊變量會將整個數組輸出到標量,然後標量可以用逗號分割,然後放入另一個數組中。 – capser

2

需要

my ($csv_html_line) = @_ ; 

作爲分配的陣列,以標量將返回它的長度(其與一個參數是1)