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

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.

  • The first case somehow gets interpreted as a bareword - and use strict; doesn't complain because there is a subroutine with that name.
  • For the second case I'm really at a loss ...
  • The rest of the cases show various ways of making it work. I'm a bit surprised as perlsub says this concerning the calling of functions
    NAME(LIST); # & is optional with parentheses. NAME LIST; # Parentheses optional if predeclared/import +ed. &NAME(LIST); # Circumvent prototypes.
    Therefore there shouldn't be any difference between get_keys() and &get_keys() as get_keys doesn't use prototypes.

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.

In reply to Sorting result of function call by Hofmator

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    choroba has a small USB lamp
    [ww]: ++ all (phony labels re "gaming"
    [1nickt]: ambrus perhaps you know where the keys are?
    [ww]: OTOH, -- for ads suggesting that being good at games is somehow eq "greatness"
    [ambrus]: 1nickg: obviously the gamers don't know. but anyway, I need the whole room backlit, because watching a monitor in a dark room (like many people do) is tiring for my eyes, even more so than doing it in a lit room.

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (18)
    As of 2017-03-27 13:55 GMT
    Find Nodes?
      Voting Booth?
      Should Pluto Get Its Planethood Back?

      Results (320 votes). Check out past polls.