develooper Front page | perl.perl5.porters | Postings from January 2011

Re: Changing variable to numeric without assignment (was: Using smart matching)

Thread Previous | Thread Next
From:
Eric Brine
Date:
January 28, 2011 10:41
Subject:
Re: Changing variable to numeric without assignment (was: Using smart matching)
Message ID:
AANLkTi=k90Fokcatfe8=JJETj+-yRBGW8V7CoWYR3ZdJ@mail.gmail.com
On Fri, Jan 28, 2011 at 4:00 AM, Abigail <abigail@abigail.be> wrote:

> > |       The auto-increment operator has a little extra builtin magic to
> > |       it. If you increment a variable that is numeric, or that has
> > |       ever been used in a numeric context, you get a normal increment.
> > |       If, however, the variable has been used in only string contexts
> > |       since it was set, and has a value that is not the empty string
> > |       and matches the pattern "/^[a-zA-Z]*[0-9]*\z/", the increment is
> > |       done as a string, preserving each character within its range,
> > |       with carry:
> >
> > (From perlop.pod; note the phrase "used in a numeric context")
>
> Note that the documentation the quote contains a couple of white lies.
>
>    $a = "foo123";
>    0 + $a;    # Use the variable $a in numeric context
>    $b = $a;
>    $a = "bar456";
>    $a ++;
>    $b ++;
>    say $a;    # Prints 'bar457';
>    say $b;    # Prints '1';
>
>
> So, we have a variable $a that's used in numeric context, and variable
> $b that isn't, yet $a gets the magic autoincrement, but $b doesn't.
>

The mistake is talking about the variable instead of the value. I think it
can be fixed by changing

If you increment a variable that is numeric, or that has ever been used in a
numeric context,

to

If you increment a value that is numeric or has ever been used in a numeric
context,

- Eric

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About