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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

In reply to pre and post increment of subroutine arguments by ig
in thread Operator Associative in Perl by irah

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (4)
    As of 2020-02-18 00:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?










      Results (74 votes). Check out past polls.

      Notices?