Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Increment avoids warning unexpectedly

by jesuashok (Curate)
on Jun 07, 2006 at 16:20 UTC ( #554082=perlquestion: print w/replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

dear monks,

#!/usr/bin/perl -w $m="a"; print ++$m; # Prints "b", as expected $m="a,b"; print $m+0; # Prints 0, as expected # throws a warning also as expected $m="a,b"; print ++$m; # Prints 1, as expected # but throws no warning
I would have expected that since the automagic increment was avoided in the third case that a warning should have been thrown that the argument "a,b" wasn't numeric.

"Keep pouring your ideas"

Replies are listed 'Best First'.
Re: Increment avoids warning unexpectedly
by davido (Archbishop) 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.


      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

      non-numeric strings are legal arguments for ++

      ++ works even on undefined values:

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

      David Serrano

      Hi davido

      Thanks a lot for your Valuable reply. It makes sense.

      "Keep pouring your ideas"
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?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://554082]
Approved by Joost
[erix]: ah! Those germans again! ... they have a lot to answer for ;-)

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2018-06-19 09:08 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.