【发布时间】:2016-02-22 11:38:48
【问题描述】:
此脚本会从下载的网页中提取网址。我在使用这个脚本时遇到了一些问题 - 当我使用 "my $csv_html_line = @_ ;" 时
然后打印出"@html_LineArray" - 它只是打印出"1's"。当我更换
"my $csv_html_line = @_ ;" 和 "my $csv_html_line = shift ;" 脚本工作正常。
我不知道"= @_" and shift 之间有什么区别 - 因为我认为
不指定任何内容,在子例程中,从"@_".shift shift
#!/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) ;
}
【问题讨论】:
标签: perl subroutine special-variables