Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Help me to understand increment / decrement operator behavior in perl

by vinoth.ree (Monsignor)
on Mar 04, 2015 at 06:07 UTC ( [id://1118714]=note: print w/replies, xml ) Need Help??


in reply to Help me to understand increment / decrement operator behavior in perl

Hi,

I have tried your code with two different variables as below, Is works as expected.

use strict; use warnings; my $a = 10; my $b = 10; #IMHO: $a-- should finish the current operation and then -1 if ($a == $b--) { print "\n T1"; } $b=10; #IMHO: --$a should -1 first and continute with current operation , i.e + if compare if ($a == --$b) { print "\n T2"; } #ENV: #Active Perl 5.20, Windows 7, x64 #Output # T2

Unary

The auto-decrement (--), and auto-increment (++) operators are unary operators. They alter the scalar variable they operate on by one logical unit. On numbers, they add or subtract one.Operators that come in post- and pre- varieties can be used two ways. The first way returns the value of the variable before it was altered, and the second way returns the value of the variable after it was altered.

my $foo = 1; # post decrement (printed and then decremented to 0) print $foo--; # prints 1 print $foo; # prints 0
my $foo = 1; # pre-decrement (decremented to 0 then printed) print --$foo; # prints 0 print $foo; # prints 0


All is well. I learn by answering your questions...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-24 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found