I'm a bit surprised by the behaviour of the following code:
use strict;
use warnings;
my %h = (1=>2, 3=>4);
sub say { print @_, $/ };
sub get_keys {
print 'called:';
if (wantarray()) {
print 'array:';
} else {
print defined wantarray() ? 'scalar' : 'void';
}
return keys %h;
}
# Output:
say sort get_keys; # get_keys
say sort get_keys(); #
say sort &get_keys; # called:array:13
say sort &get_keys(); # called:array:13
say sort @{[get_keys]}; # called:array:13
say sort @{[get_keys()]}; # called:array:13
I would have expected that all these outputs would be identical. Here my guesses what went wrong. Could somebody shed some more light on it, please.
In case it matters: This is perl, v5.6.1 built for i386-linux.
And btw, I came across this when I tested App::Ack (Version 1.50) and tried to run ack --help types which doesn't display any types for me. Tracking it down, I reached the following line in App::Ack::show_help_types, which exhibits the behaviour of case 2 above.
for my $type ( sort( filetypes_supported() ) ) {
-- Hofmator
Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
-
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 How to display code and escape characters
are good places to start.
|