Perl gotcha’s–equality

If you try to compare strings using ==, and you have included the use warnings pragma, you will get a warning, as in the example below. However, Perl will still attempt to convert the string into a number. If the string starts with numbers, Perl will use these, otherwise the string equates to 0.

So my conditional,


if ($device_id == '5G0E3663') { ... }

compares two zeros and always returns true. Which in my case made bad things happen.
The correct way to compare strings is:


if ($device_id eq '5G0E3663') { ... }

h/t perlmeme.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.