cap*_*ser 0 perl subroutine special-variables
此脚本从下载的网页中删除网址.我在使用这个脚本时遇到了一些麻烦 - 当我使用它"my $csv_html_line = @_ ;"
然后打印出来时"@html_LineArray"- 它只是打印出来"1's".当我更换
"my $csv_html_line = @_ ;"使用"my $csv_html_line = shift ;"脚本工作正常.我不知道有什么不同之处"= @_" and 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) ;
}
Run Code Online (Sandbox Code Playgroud)
这就是以上打印出的内容.
1
1
1
1
1
1
1
1
1
1
1
1
Run Code Online (Sandbox Code Playgroud)
这很好 - 它使用移位而不是"@_"
#!/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) ;
}
Run Code Online (Sandbox Code Playgroud)
它的
my ($csv_html_line) = @_ ;
Run Code Online (Sandbox Code Playgroud)
您编写@_在标量上下文中评估的代码并获得其长度(元素数)的方式.如你所说,
my $csv_html_line = shift;
Run Code Online (Sandbox Code Playgroud)
因为shift运算符获取列表并删除并将第一个元素作为标量返回,因此可以正常工作.
| 归档时间: |
|
| 查看次数: |
148 次 |
| 最近记录: |