问题描述
此脚本从下载的网页中提取网址.我在使用这个脚本时遇到了一些问题 - 当我使用 "my $csv_html_line = @_ ;"
然后打印出 "@html_LineArray"
- 它只是打印出 "1's"
.当我更换"my $csv_html_line = @_ ;"
与 "my $csv_html_line = shift ;"
脚本工作正常.我不知道 "= @_" 和 shift
之间有什么区别 - 因为我认为不指定任何东西,在子程序中,从 "@_".
This script rips out the urls from a downloaded webpage. I had some trouble with this script - when I use the "my $csv_html_line = @_ ;"
and then print out the "@html_LineArray"
- it just prints out "1's"
. When I replace the"my $csv_html_line = @_ ;"
with "my $csv_html_line = shift ;"
the script works fine.I do not know what the difference is betweeh the "= @_" and shift
- becuase I thought thatwithout specifying something, in a subroutine, shift shift froms "@_".
#!/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
这很好用 - 它使用移位而不是@_"
This works fine - it uses the shift instead of "@_"
#!/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) ;
}
推荐答案
It's
my ($csv_html_line) = @_ ;
您编写代码的方式在标量上下文中评估 @_
并获得其长度(元素数).正如您所指出的,
The way you wrote the code you evaluated @_
in scalar context and got its length (number of elements). As you noted,
my $csv_html_line = shift;
之所以有效,是因为 shift
运算符接受一个列表并删除并以标量形式返回第一个元素.
works because the shift
operator takes a list and removes and returns the first element as a scalar.
这篇关于Perl 特殊变量“@_"在一个不工作的子程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!