in reply to Re: Understanding Perl context (removing duplicates from array)
in thread Understanding Perl context

A hash slice initializing undef values is better written as @unordered{@array}=() , assigning undef to the first element is misleading! [emphasis added]

IMHO, this statement is itself a bit misleading. The two forms of the statement are exactly equivalent! The effect of either  () or  undef as the RHS of the assignment is exactly the same because both are equivalent to lists of  undef values of exactly the same length as the  @array array. This slightly obscure point must be understood regardless of the assignment used, and neither assignment seems to me better suited to making this point.


Give a man a fish:  <%-(-(-(-<

Replies are listed 'Best First'.
Re^3: Understanding Perl context (removing duplicates from array)
by LanX (Saint) on Apr 19, 2015 at 13:29 UTC
    Its misleading, because the next maintainer might think he can replace undef with 0 and bang!

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Hmmm... Hadn't thought about that.

      But in the specific case of the OPed code, replacing  undef with 0 (or, indeed, with 1 or any other value) would make no difference: you still end up with a hash of unique keys.

      And in the general case, if code did depend on the difference between  undef and 0 as values, would not this dependence be underscored by the explicit use of  undef as the initializer (reinforced, one would hope, by a comment) rather than the more general  () empty list?


      Give a man a fish:  <%-(-(-(-<

        Sorry, I've had too many coworkers in the past using broken copy and paste code and justifying that the cargo cult worked so far.

        ("Of course it's well tested! I ran it once and nothing bad happened ... " )

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!