in reply to The trap of reference numification

I partially agree. Any math operation (excluding ==) on a reference that doesnt have the references nummified by 0+$ref and doesnt have the operators in question overloaded in the package the ref is blessed into should warn. Anything else should not, as it allows the author to signal "yes I am doing this on purpose" and as 0+$ref is such a common idiom (albeit a moderately dangerous one due to overloading). Likewise you don't want warnings flying everywhere when you are using bigint objects with overloading.

---
$world=~s/war/peace/g

Replies are listed 'Best First'.
Re^2: The trap of reference numification
by Perl Mouse (Chaplain) on Nov 14, 2005 at 10:37 UTC
    Any math operation (excluding ==) on a reference that doesnt have the references nummified by 0+$ref and doesnt have the operators in question overloaded in the package the ref is blessed into should warn.
    I disagree. That's contrary to Perl's automatic casting for programmer convenience. What's next, concatenation of numbers throws a warning, unless the number is surrounded with quotes? Use of an array as a number throws a warning, unless it's explicitely preceeded with '0+'? Using a string as first argument of split throws a warning, because it isn't surrounded by slashes?

    If you want a language that will throw warnings if casting happens implicitely, use C.

    I don't think that the attitude of "oh, I made a programming boo-boo, let's add a warning that prevents me from making the boo-boo again" is very productive. It will only annoy people who legitimate use this feature (that has been around for over a decade!) - and it will make people not use use warnings;. Or to not use perl.

    Warnings are good. But a warning that triggers at the wrong moment is bad. Very bad. Warnings should never get in the way.

    Perl --((8:>*
      $ perl -wle'print "99 bottles" + 1'
      Argument "99 bottles" isn't numeric in addition (+) at -e line 1.
      100
      

      Makeshifts last the longest.

      Well, Perl has a tendency to take the attitude that it should warn when you are most likely doing something stupid, and to not to warn when you probably aren't. Thus you see things like "0 but true" NOT warning

      C:\Temp>perl -we"print 0+'0 but true'" 0 C:\Temp>perl -we"print 0+'0 and true'" Argument "0 and true" isn't numeric in addition (+) at -e line 1. 0

      Since I can't think of any remotely sane reason why one would do a string or numeric comparison other than '==' or 'eq' on a ref (ok, I lie, I can think of one, but its pretty sucky, that being that you can sort references into an arbitrary but repeatedable order using reference comparison), therefore it doesnt seem to me to be unreasonable to warn in these situations. Whereas 0+ implies the decision was intentional and not accidental, and overloading implies that the operation is specifically allowed, and '==' is the normal way for doing equivelency tests on refs, so you dont want to warn in those situations.

      What would this really mean for the programmer? Well it means that when they want to violate the encapsulation that references provide and use an arbitrary value (the memory location of the ref) for something it probably shouldn't be used for they need to signal to the interpreter that they would like to blow their foot off, by using the 0+ disambiguation. IMO this would probably catch a few bugs, and maybe break some code that uses references as hashkeys without using the 0+ disambiguation, something that could itself be special cased, or probably better just be worked around with a 0+.

      Anyway, I doubt any of this will ever happen, but I thought I would express what I consider to be the way it might make sense.

      ---
      $world=~s/war/peace/g