# Help with operator precedence

• 01-23-2014, 02:22 PM
maxwell55
Help with operator precedence
Hi
I am having a bit of a problem working out operator precedence in the follow expression, the output of which is 0.
I realize ++\$value2 is incremented immediately so both ++\$value2s should now contain 3,
and \$value1-- is not decremented immediately so contains 8, which should make the expression
8 % 3 * 3 which when run mysteriously returns 6, 8 / 3 has remainder 2, 2 * 3 = 6, I had a look at a perl operator
precedence chart, which said * is before % this would make the expression 8 % 9

Code:

```\$value1 = 8; \$value2 = 2; \$value3 = \$value1-- % \$value2 * ++\$value2; print (\$value3); output: 0```
I also tried to see how it calculated by enclosing the expression in double quotes which returned the following -

Code:

```print "\$myvar1-- % \$myvar2 * ++\$myvar2"; output:  8-- % 2 * ++282```
• 01-24-2014, 06:33 AM
Sixtease
There's a neat module called B::Deparse, that can show you how exactly the perl interpreter understands your code. I saved the code in a file called precedence.pl and called the following line and getting the following output:
Code:

```perl -MO=Deparse,-p precedence.pl (\$value1 = 8); (\$value2 = 2); (\$value3 = (((\$value1--) % \$value2) * (++\$value2))); print(\$value3);```
This means you get 8%2, which is 0 times whatever, so obviously 0; Only after the multiplication is carried out, the increment takes place, though the incremented value is used. So you have ((8%2)*3)

The quote approach can of course not work, because you only get the variables interpolated and not the operators.
• 01-24-2014, 06:54 AM
Sixtease
By the way, the difference between saying \$foo++ vs ++\$foo is not that ++\$foo is somehow incremented sooner or "immediately". It merely means that the expression returns the original or the incremented value respectively.
• 01-24-2014, 12:43 PM
maxwell55
Hi
As suggested I downloaded the module from cpan as it wasn't available in the Activeperl package manager.
I am sure it will be invaluable to me. Also the syntax used must be exact perl -MO=Deparse,-p precedence.pl
the first time I ran it I mistakenly included a space between the , - and got totally different output.

Best regards Maxwell

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.10278 seconds
• Memory Usage 2,348KB
• Queries Executed 11 (?)
Template Usage (20):
• (3)bbcode_code_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates