Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Bachelor of Arts?

by trs80 (Priest)
on Aug 26, 2002 at 04:21 UTC ( #192765=note: print w/replies, xml ) Need Help??


in reply to Bachelor of Arts?

This oddity is documented in perldoc perlop. The autoincrement operator has this effect, but the autodecrement operator however is not magical.

Replies are listed 'Best First'.
Re: Re: Bachelor of Arts?
by BrowserUk (Pope) on Aug 26, 2002 at 14:25 UTC

    I was aware and making use of the magical effects of ++ on strings.


    What's this about a "crooked mitre"? I'm good at woodwork!
      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:
      my $a='A'; print $a , $a+1 ;
      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:
      my $a='A'; print $a . $a+1 ;
      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.
      my $a='A'; print "$a" . $a+1; </cdoe> Same thing, still a single '1'. <br> <br> How about if we change it back to list context? <code> my $a='A'; print "$a" , $a++ ;
      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.

      my $a='A'; print "$a", $a++ ; print $a;
      Now we get 'AAB'

      Update I see now that dws has a much more learned response then mine.

        You went through a subset of the things I did to try and isolate this obfu writers dream before I gave up, posted and went to bed.

        The original line of code that brought this to my attention (read:What the *&£*&"% is going on here!!!!!) had two post-incremented strings, one twice, in a double-mapped, for modified array assignment (I was generating yet another set of test data).

        Needless to say, when I went and looked at the output, I was more than bemused.

        The best and nicely concise explanation of the how's and why's is dws's post. Made nice reading first thing this morning, to discover I wasn't loosing my sanity (what little I have left:).

        Cheers.


        What's this about a "crooked mitre"? I'm good at woodwork!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2020-07-06 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?