Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Increment avoids warning unexpectedly

by davido (Cardinal)
on Jun 07, 2006 at 16:36 UTC ( #554086=note: print w/replies, xml ) Need Help??


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 (Pope) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://554086]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2020-02-21 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (96 votes). Check out past polls.

    Notices?