in reply to Why does this hash remove duplicate lines

 $lines{$_} is a hash entry, with the value of $_ this time through the loop as the key.

 $lines{$_}++ is incrementing the value of the hash entry with the key of $_.

Since you're creating the key the first time you see a specific line, the value for that key is undefined. The first time you increment an undefined value in Perl you get 1.

 print if not $lines{$_}++; Undefined values are false, and 1 or higher are determined to be true values in boolean context.

Basically, it's saying "Note this line. Note this line has been seen again if there's already a note about it. Print the line if it has not been seen before."

The reason order is preserved is simply because the printing is happening at the time the line is seen and the while loop and diamond operator are reading the lines in order.

As for whether you can use unless, that's probably simple enough for you to try and see, isn't it? ;-)

Replies are listed 'Best First'.
Re^2: Why does this hash remove duplicate lines
by chromatic (Archbishop) on Mar 06, 2008 at 08:18 UTC
    Undefined values are false, and 1 or higher are determined to be true values in boolean context.

    Undefined values, 0, and the empty string are false. Everything else is true in boolean context -- even negative numbers.

      Yes, that's true. Negative numbers happen to be irrelevant to the example, which is the reason I didn't mention them. The clarification may help, but I didn't see that as the OP's source of misunderstanding the example.