(regarding $hashref->{-option})
Interestingly, the former is actually interpreted as an expression (note the "negate" below),...

Thanks for providing another interesting insight!

We've moved pretty far from the original issue, so I've modified the title... and I allow myself to add some stuff I found while experimenting with bareword stringification (and, just for fun, abusing some not directly related pitfalls into which I fell some time ago). Warning: This isn't to be taken too seriously. Read on at your own risk.

Consider the following code:

use strict; use warnings; use Data::Dump qw(dump); $_ = $0; my %h; sub f { print('->f()') . '.0'; } $h{ f } = ' F'; $h{ +f } = ' +F'; $h{+ f } = '+ F'; $h{ -f } = ' -F'; $h{- f } = '- F'; $h{ f()} = ' ()'; $h{ -f()} = '-()'; $h{- f()} = '-()'; $h{ +f()} = '+()'; print "\n",dump(\%h),"\n";
This prints:
->f()->f()->f()->f()->f() { "-1" => "-()", "-f" => "- F", "1" => "-()", "1.0" => "+()", "f" => " + F" }

The resulting hash %h has five keys, and the function f has been called five times. Before going into the boring stuff, see the following code, which is equivalent... well, almost:

use strict; use warnings; use Data::Dump qw(dump); $_ = $0; sub f { print('->f()') . '.0'; } my %h = ( f => ' F', +f => ' +F', + f => '+ F', -f => ' -F', - f => '- F', f() => ' ()', -f() => '-()', - f() => '-()', +f() => '+()', ); print "\n",dump(\%h),"\n";
...which prints...
->f()->f()->f() { "-1" => "-()", "-f" => "- F", "1" => "-()", "1.0" => "+()", "f" => " ++ F" }

This time I've used "fat comma" stringification which got me the same keys, but only three calls to function f.

So, these are the questions:

  1. Why are there five keys in the hash, for eight apparently different expressions?
  2. Which of the key definitions don't cause function f to be called in the second example?
  3. Why is there a different value for $h{'f'}?
  4. Why is there an odd number of calls to the function f in both examples?
  5. Where do the keys -1, 1, and 1.0 come from?

I always wanted to test that Perlmonks' spoiler element, so here it is:


In reply to Barewords with unary operators (was: Problem with perlcritic...) by haj
in thread Problem with perlcritic when using Moose object by bangor

Title:
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, details, 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, summary, 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.