Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re^9: Using 'keys' on a list (update x2) by glycine
in thread Using 'keys' on a list by lammey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2022-05-28 21:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?