2016-02-22 23 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)