Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) 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:
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.

In reply to Re: Re: Re: Bachelor of Arts? by trs80
in thread Bachelor of Arts? by BrowserUk

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-29 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found