|There's more than one way to do things|
Re: Re: Re: Bachelor of Arts?by trs80 (Priest)
|on Aug 26, 2002 at 15:05 UTC||Need Help??|
I thought you might be :)
As I drifted off to sleep last night this node haunted me. It isn't the magical ++ , but more so its effect of the order of precedence, or at least that is what I thought since your example code was passing variables in a list context I decided to run it this morning in a string context ( '.' vs. ',' ) and the results are the same. It seems that the magic is deep and order in which the operations occur is contray to what is expected.
Possibly the depth of the magic can be better demostrated with with this code:
Now we get 'A1' , which is completely confusing after looking at what the autoincrement operator did, but the autoincrement operator does a string evaluation, where as the +1 is numerical. ++ is deep magic it appears.
To further confuse the issue we try it in a string context versus list:
Now we get just a '1', this just gets odder and odder. But wait what if we do this in a string and "stress" $a as a string as well.
Hey now we get "AA".
So what is doing on? Did ++ lose it magic?
Yes and no. In the original bit of code submitted Perl is doing some deep magic by executing the ++ while processing the list and then creating the output for print, that is 'A' is incremented first and the ++ is POST value, since ++ has precedence it is processed and the original value placed into the out going string, then the value of $a is examined, since ++ has incremented its value it now sees a 'B' post ++.
But what about the "$a" difference? The magic isn't lost it simply doesn't apply because of precedence, a string value is interpeted prior to a numerical (or implied numerical operation) which is what perl sees with the unquoted $a.
Now we get 'AAB'
Update I see now that dws has a much more learned response then mine.