下面的 Perl 代码运行到一个无限循环中。它看起来像each
在子例程调用后自行重置。为什么呢?
#!/usr/bin/perl
use warnings;
use strict;
my %h = ( "a" => "b" );
while ( my ($x, $y) = each %h ) {
&do_something( \%h );
}
sub do_something(){
my %tmp = %{$_[0]};
}
有趣的是,这有效:
while ( my ($x, $y) = each %h ) {
&do_something( \%h );
}
sub do_something(){
}
虽然这不会:
while ( my ($x, $y) = each %h ) {
&do_something( %h );
}
sub do_something(){
}