I understand, and you're right of course. This doesn't have to do with the semantics of the statement. It's an interesting case, and I think it illustrates that the logic for generating this warning is simplistic (as chromatic points out). In fairness, I think that's the intent of warnings: They aren't errors, they simply bring your attention to something that, in the many cases, may indicate a potential error. In this particular case, you know better, so the warning doesn't make sense.

If you do decide to use an alternative syntax, FWIW I'd probably use an alternative syntax like:
if (not $P::x) { $P::x = 1; f(); }
Not only is this considerably more understandable, IMO, but I ran some benchmarks, and the speed difference between this and the form using ||= is negligible. On the other hand, using the expanded syntax with || is (surprisingly!) slower.
use strict; use warnings; use Benchmark qw( timethese cmpthese ); my $results = timethese( 5000, { orig => sub { delete $P::x{some_key}; for ( 1 .. 1000 ) { $P::x{some_key} ||= ( f(), 1 ); } }, alt_1 => sub { delete $P::x{some_key}; for ( 1 .. 1000 ) { $P::x{some_key} = $P::x{some_key} || ( f(), 1 ); } }, alt_2 => sub { delete $P::x{some_key}; for ( 1 .. 1000 ) { if ( not $P::x{some_key} ) { $P::x{some_key} = 1; f(); } } }, } ); cmpthese($results); sub f() { #No op. }
Benchmark: timing 5000 iterations of alt_1, alt_2, orig... alt_1: 3 wallclock secs ( 3.08 usr + 0.00 sys = 3.08 CPU) @ 16 +23.38/s (n=5000) alt_2: 2 wallclock secs ( 1.29 usr + 0.00 sys = 1.29 CPU) @ 38 +75.97/s (n=5000) orig: 1 wallclock secs ( 1.25 usr + 0.00 sys = 1.25 CPU) @ 40 +00.00/s (n=5000) Rate alt_1 alt_2 orig alt_1 1623/s -- -58% -59% alt_2 3876/s 139% -- -3% orig 4000/s 146% 3% --

In reply to Re^5: Why do I get a "used only once" warning here? by bellaire
in thread Why do I get a "used only once" warning here? by rovf

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.