http://qs1969.pair.com?node_id=554086


in reply to Increment avoids warning unexpectedly

The pre-increment and post-increment operators are designed to work on strings too. Witness the following:

my $string = "abc"; print ++$string, "\n";

If my calculations are correct, you should see "abd".

So the point is that if you give ++ a string that it can figure out, it will increment it. Thus, non-numeric strings are legal arguments for ++. Now, in your case, you gave the increment operator a string that it couldn't sensibly increment in its non-numeric form, so the ++ operator dropped into numeric form, and incremented 0 to 1. No warning because non-numeric strings are legal in this case.

To read more about the ++ operator's "magic", see perlop.


Dave

Replies are listed 'Best First'.
Re^2: Increment avoids warning unexpectedly
by ikegami (Patriarch) on Jun 07, 2006 at 17:23 UTC

    I see two holes in your argument.

    1) That particular string is non-numeric and it does get converted to numeric. What would be the point of not issuing a "non-numeric isn't numeric" warning when it happens because the operator works with some other strings?

    2) perl -wle "$s='abc'; $s--; print $s" doesn't give a warning either, yet non-numeric strings are never legal for the post-decrement operator.

    I suspect the warning is surpressed as a side-effect (intentional or not) of supressing the undefined warning when executing my $i; $i++;.

Re^2: Increment avoids warning unexpectedly
by jpeg (Chaplain) on Jun 07, 2006 at 16:45 UTC
    However, the ++'s operator's magic applies to strings that =~ m/^[a-zA-Z]*[0-9]*\z/. "a,b" doesn't match.

    I suspect that in the third case perl is calling int($m), which returns 0, then applying the prefix increment operator.

    Code tags added by GrandFather

    --
    jpg
Re^2: Increment avoids warning unexpectedly
by Hue-Bond (Priest) on Jun 23, 2006 at 22:00 UTC
    non-numeric strings are legal arguments for ++

    ++ works even on undefined values:

    $ /usr/bin/perl -le'print $a++' 0

    --
    David Serrano

A reply falls below the community's threshold of quality. You may see it by logging in.