Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

pre and post increment of subroutine arguments

by ig (Vicar)
on Jun 04, 2009 at 21:37 UTC ( #768598=note: print w/replies, xml ) Need Help??


in reply to Re: Operator Associative in Perl
in thread Operator Associative in Perl

I find the difference between $a++ and ++$a as subroutine arguments to be quite interesting/surprising.

In a subroutine the elements of @_ are usually aliases to the arguments of the subroutine call, allowing the subroutine to modify the caller's variables.

In the case of ++$a, the corresponding element of @_ is a reference to the same IV as $a but in the case of $a++ it is a reference to a new IV, effectively preventing the subroutine from modifying the caller's variable. In both cases the array element is an lvalue (i.e. it can be modified), but only in the case of pre-increment can the subroutine modify the caller's variable.

Using pre/post increment/decrement on an argument to a subroutine that attempts to modify that argument is not a good thing to do because the order of execution is not defined. None the less, the difference in implementation is interesting.

use strict; use warnings; use Devel::Peek; sub subx { Dump(\@_); $_[0] += 10; Dump(\@_); } my $a = 5; Dump($a); subx(++$a); Dump($a); subx($a++); Dump($a); __END__ SV = IV(0x9ba875c) at 0x9ba8760 REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 5 SV = RV(0x9b981fc) at 0x9b981f0 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x9ba8780 SV = PVAV(0x9b9928c) at 0x9ba8780 REFCNT = 3 FLAGS = () ARRAY = 0x9bacbd8 FILL = 0 MAX = 3 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = IV(0x9ba875c) at 0x9ba8760 REFCNT = 2 FLAGS = (PADMY,IOK,pIOK) IV = 6 SV = RV(0x9b981fc) at 0x9b981f0 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x9ba8780 SV = PVAV(0x9b9928c) at 0x9ba8780 REFCNT = 3 FLAGS = () ARRAY = 0x9bacbd8 FILL = 0 MAX = 3 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = IV(0x9ba875c) at 0x9ba8760 REFCNT = 2 FLAGS = (PADMY,IOK,pIOK) IV = 16 SV = IV(0x9ba875c) at 0x9ba8760 REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 16 SV = RV(0x9b981fc) at 0x9b981f0 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x9b98340 SV = PVAV(0x9b9923c) at 0x9b98340 REFCNT = 3 FLAGS = () ARRAY = 0x9bb7888 FILL = 0 MAX = 3 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = IV(0x9bb541c) at 0x9bb5420 REFCNT = 2 FLAGS = (PADTMP,IOK,pIOK) IV = 16 SV = RV(0x9b981fc) at 0x9b981f0 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x9b98340 SV = PVAV(0x9b9923c) at 0x9b98340 REFCNT = 3 FLAGS = () ARRAY = 0x9bb7888 FILL = 0 MAX = 3 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = IV(0x9bb541c) at 0x9bb5420 REFCNT = 2 FLAGS = (PADTMP,IOK,pIOK) IV = 26 SV = IV(0x9ba875c) at 0x9ba8760 REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 17

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2020-02-28 16:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (125 votes). Check out past polls.

    Notices?