You'll notice that the value printed isn't the num of keys but a key itself. (Suppose the code from the beginning to end of foo function is located in an external package!!) But foo return a list; the value returned by foo is evaluated in an array context and so 'Why does not it work?'my %hash = ( a => 1, b => 2, c => 3, d => 4 ); sub foo { my $n = ($_[0]) ? $_[0] : keys %hash; return (keys %hash)[0 .. ($n - 1)]; # return %hash keys from 0 to $n + - 1 } my $bar = &foo(); # want to return the number of keys of %hash print "$bar\n";
In reply to List context or not? by sguazt
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |