I was looking for something in perlop when I noticed this in the description for the tr/// operator:

If no string is specified via the =~ or !~ operator, the $_ string is + transliterated.

Never having thought of using !~ in conjunction with tr///, I wondered what it did, so I tried a few things:

C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[a][]" C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[b][]" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[bb][]" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[bc][]" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[b][]c" C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[b][]d" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[b][]" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[a][]c" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[ab][]" C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[a][]" C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[b][]" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[c][]" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[bc][]" 1 C:\test>perl -wle"$_ = 'aab'; print $_ !~ tr[bc][]"

From which I concluded that in this 'inverted count-the-stars' mode, it returns a true or false value indicating whether the string doesn't contain any of the characters in the searchlist. Not exactly intuative, but could be useful sometime. So then I thought I'd try the delete flag, and the result surprised me no end.

Can you guess what this would do before you try it?

perl -wle"$_ = 'aaa'; $_ !~ tr[a][]d; print"

Were you right?

Can you explain it ;)


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re: Guess the output.
by ikegami (Patriarch) on Sep 27, 2006 at 03:33 UTC

    Can you guess what this would do before you try it?

    Yes.

    I couldn't see why it would print anything but an empty string, and I suspected it would give the "void context" warning.

    a !~ b is the same thing as !(a =~ b). I don't know if that's intuitive or not (since I learned its meaning long ago), but it's well documented. Since a =~ b returns true when characters were replaced or deleted, a !~ b returns true when no characters are replaced or deleted. Your snippet prints an empty string because tr[a][]d executes as normal. Only the return value is negated.

    Furthermore, the negated value is discarded. When the return value of an action with no side effects is discarded, you should you get a "void context" warning. The following give void context warnings as well: (from least obvious to most obvious)

    • perl -we "$a !~ $b"
    • perl -we "!($a =~ $b)"
    • perl -we "!($a = $b)"

    Update: Rearranged the post (including earlier updates) so more would be behind the spoiler tag.