http://qs1969.pair.com?node_id=11134530


in reply to Re^8: Using 'keys' on a list (update x2)
in thread Using 'keys' on a list

modified: my fault, I do this on one line, and I rewrote code when I post them, I trust they are same.
apparently, they are not.

and I find I should just quote this:"Called in list context, ...", from perldoc...

Only focus on op's condition...

use strict; use warnings; use feature 'say'; sub f {a => 1, b=>2} my $foo = f(); my $bar = join '_', f(); say $foo; say $bar; #outputs 2 a_1_b_2

so that's why I assume the first argument keys accept is 'a', like join.
I just want to explain keys don't work for that single scalar value.

but but... keys looks like do not accept arguments in scalar context
if we write:

sub f {a => 1, b=>2} my @all_key = keys f();
the warning message is "Type of arg 1 to keys must be hash or array (not subroutine entry)"
so I tried it:
sub f {a => 1, b=>2} my %h = (c => 3); say keys %h, f(); #output: c
and then if we write:
sub f {a => 1, b=>2} my %h = (c => 3); say keys f(), %h; #warnings: Experimental keys on scalar is now forbidden at perl.pl line 11. Type of arg 1 to keys must be hash or array (not subroutine entry) at +perl.pl line 11, near ")," Execution of perl.pl aborted due to compilation errors.
so, this is why I think it is 'a'.
I guess the first argument of keys is the first element in list.
p.s. I should come back earlier...


edit: my fault, I type '}' as ')' ... thanks The Perlman and LanX, I had modified them:(
yes, I find every result different now. I write them on one line... here I redo all:

use strict; use warnings; use feature 'say'; sub f {a => 1, b=>2} my $foo = f(); my $bar = join '_', f(); say $foo; say $bar; #output 2 a_1_b_2 sub f {a => 1, b=>2} my @all_key = keys f(); #warnings: Experimental keys on scalar is now forbidden at call_art.pl line 7. Type of arg 1 to keys must be hash or array (not subroutine entry) at +call_art.pl line 7, near ");" Execution of call_art.pl aborted due to compilation errors. sub f {a => 1, b=>2} my %h = (c => 3); say keys %h, f(); #output ca1b2 sub f {a => 1, b=>2} my %h = (c => 3); say keys f(), %h; #warnings: Experimental keys on scalar is now forbidden at call_art.pl line 8. Type of arg 1 to keys must be hash or array (not subroutine entry) at +call_art.pl line 8, near ")," Execution of call_art.pl aborted due to compilation errors.