펄에서, 당신은 서브 루틴 스칼라의 목록을 통과 확인.
스칼라 값이 가능
- 기본 데이터 타입, 즉. String, Int, double
- 배열에 대한 참조입니다. 서브 루틴을 호출 할 때
my @list = (1, 2, 3);
#note the variable declaration being surrounded by parenthesis.
#that says use list context
sub getList {
my ($foo, $bar, $baz) = @_; #(@_ is the list you passed to the sub)
}
이제 (배열 스칼라의 목록이기 때문에) 당신이이 개 목록이있는 경우
- 해시에 대한 참조는
그래서, 당신은, 전체 배열을 전달할 수 있습니다 두 목록이 @_
에 포함 된 하나의 배열로 평평하게 될 것입니다 때문에 당신은, 당신이
getList(@listA, @listB);
을 할 수없는 서브 루틴에 전달하려는.
그래서이 문제를 해결하려면 참조를 사용하거나 배열을 참조로 전달하십시오.
my $first_list = \@listA #this points $first_list to a reference in
#memory where @listA is located.
getList($first_list, \@listB);
sub getList {
my ($foo, $bar) = @_; #$foo and $bar now point to @listA and @listB
#To dereference these, you either have to use the -> operator to access
#Elements, or use the form @{ }. %{ } for hashes.
my @listA = @{ $foo };
my @listB = @{ $bar };
#Now they are regular arrays again!
}
오. 이것은 매우 유익하고 유익했습니다. 놀라운 대답! 나는 Perl의 멋진 문법을 즐기며 전반적으로 스크립트 언어의 팬이되어 가고있다. 고맙습니다! –
목록에없는 링크의 +1은 매일 새로운 것을 배웁니다! – Gabs00