The value 0 but true is a special case in Perl. Although to your mere mortal eyes, it doesn't look like a number, wise and all knowing Perl understands it really is a number.

It has to do with the fact that when a Perl subroutine returns a 0 value, it is assumed that the routine failed or returned a false value.

Imagine I have a subroutine that returns the sum of two numbers:

die "You can only add two numbers

" if (not add(3, -2)); die "You can only add two numbers

" if (not add("cow", "dog")); die "You can only add two numbers

" if (not add(3, -3));

The first statement won't die because the subroutine will return a 1 . That's good. The second statement will die because the subroutine won't be able to add cow to dog.

And, the third statement?

Hmmm, I can add 3 to -3 . I just get 0 , but then my program will die even though the add subroutine worked!

To get around this, Perl considers 0 but true to be a number. If my add subroutine returns not merely 0, but 0 but true, my third statement will work.

But is 0 but true a numeric zero? Try these:

my $value = "0 but true"; print qq(Add 1,000,000 to it: ) . (1_000_000 + $value) . "

"; print "Multiply it by 1,000,000: " . 1_000_000 * $value . "

";

Yup, it's zero!

The index subroutine is a very old piece of Perl and existed before the concept of 0 but true was around. It is suppose to return the position of the substring located in the string:

index("barfoo", "foo"); #This returns 3 index("barfoo", "bar"); #This returns 0 index("barfoo", "fu"); #This returns ...uh...

The last statment returns a -1 . Which means if I did this:

if ($position = index($string, $substring)) { print "It worked!

"; } else { print "If failed!

"; }

As I normally do with standard functions, it wouldn't work. If I used "barfoo" and "bar" like I did in the second statement, The else clause would execute, but if I used "barfoo" and "fu" as in the third, the if clause would execute. Not what I want.