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

jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Increment avoids warning unexpectedly
by davido (Cardinal) on Jun 07, 2006 at 16:36 UTC

    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

      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++;.

      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
      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.
Re: Increment avoids warning unexpectedly
by MonkE (Hermit) on Jun 07, 2006 at 17:07 UTC
    The autoincremenet operator is "magical", see perlman:perlop for more details. Try adding the following code to your test program to see the magic at work:
    $m="acb"; print ++$m; # Prints acc, as expected (according to perlop) # and throws no warning ... wierd huh?