Although a documented one... basically I just want to report an interesting entry in the Journal of DAxelrod (who is also a fellow monk here), at use Perl.

To cut it short, the following:

#!/usr/bin/perl use strict; use warnings; my ($num, $let, $foo); $num = "z"; $let = $num; # $foo = 1 + $num; # this does not modify $num $num++; $let++; print "num:$num let:$let\n"; __END__

outputs

num:aa let:aa

whereas if you uncomment line 9, it will issue a

Argument "z" isn't numeric in addition (+) at C:\temp\foo.pl line 9.

warning, and output

num:1 let:aa

which is as of the subject, surprising and unexpected due to the fact that $num is just used, but appearently not modified in any way whatsoever. However, as the original article itself says this is both documented and useful for obfu. In this respect I was thinking that a particularly confusing effect could be achieved if one used e.g. "9" instead of "z", but that doesn't seem to be the case:

C:\temp>perl -le "$a='z';print++$a" aa C:\temp>perl -le "$a='9';print++$a" 10

B::Concise seems to say the same for both:

C:\temp>perl -MO=Concise -e "$a='z'" >z -e syntax OK C:\temp>cat z 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const[PV "z"] s ->4 - <1> ex-rv2sv sKRM*/1 ->5 4 <#> gvsv[*a] s ->5 C:\temp>perl -MO=Concise -e "$a='9'" >9 -e syntax OK C:\temp>diff z 9 5c5 < 3 <$> const[PV "z"] s ->4 --- > 3 <$> const[PV "9"] s ->4

(Note: damn! I couldn't use | diff z - under Windows.)

Fortunately, this is perfectly well documented as well:

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.

This obviously mean that "that is numeric" comprises "that looks like a number". How much it may resemble one, though, is not very intuitive. I do know it's more or less Perl's standard behaviour, but I for one would expect the string to be treated as a string in both of the following:

C:\temp>perl -le "$a='foo9';print++$a" fop0 C:\temp>perl -le "$a='9bar';print++$a" 10

In reply to A somewhat unexpected side effect... by blazar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.