With all this talk of scalar, I had to look it up because, despite using Perl heavily for twenty years, I've never actually used it! Which reminded me that I have employed the equivalent "secret" version, namely the infamous inchworm ~~ secret operator

Just a little nitpick: the "inchworm" isn't exactly equivalent, as it doesn't pass through lvalue context, which scalar does as of 5.22.

$ perlbrew exec perl -e 'for(scalar($#foo)) { $_=3 } warn "$] ".@foo." +\n"' >/dev/null 5.034000 4 5.032001 4 5.030003 4 5.028003 4 5.026003 4 5.024004 4 5.022004 4 5.020003 0 5.018004 0 $ perlbrew exec perl -e 'for(~~$#foo) { $_=3 } warn "$] ".@foo."\n"' > +/dev/null 5.034000 0 5.032001 0 5.030003 0 5.028003 0 5.026003 0 5.024004 0 5.022004 0 5.020003 0 5.018004 0

Update: The example above is one of the exceptions for when ~~ is not equivalent to scalar anyway:

$ perl -le 'print scalar($#foo)' -1 $ perl -le 'print ~~$#foo' 18446744073709551615

... but the point still stands (though interestingly, my $foo="x"; for(scalar($foo)) {$_.="y"} worked even before 5.22).


In reply to Re^3: How to test for empty hash? by haukex
in thread How to test for empty hash? by scareduck

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.