In short, "don't do that". But if you're interested in the internal details, read on.

Values in Perl are stored in a struct call "SV" (or a derivative thereof). SVs aren't copied around. Pointers to SVs are. That results in "pass by reference" being the default model for every operator, function and sub. This creates interesting side-effects as seen in the following:

>perl -le"$x=3; print($x+0, $x, ++$x, $x, $x++, $x, $x+0);" 3555455

$x and ++$x place a pointer to $x's SV on the stack. $x+0 (and "$x" for strings) creates a new SV, so it's no affected by later changes to $x. Similarly, $x++ creates a new SV containing a copy of what $x was at the time of the increment.

Equivalent code in C++:

#include <stdio.h> void print(const int &g, const int &f, const int &e, const int &d, con +st int &c, const int &b, const int &a) { printf("%d%d%d%d%d%d%d\n", a, b, c, d, e, f, g); } int main() { int x = 3; print(0+x, x, x++, x, ++x, x, 0+x); // 5 5 4 5 5 5 3 return 0; }

(++x and 0+x produce constants in C++, but not in Perl.)


In reply to Re^11: Setting signal handlers considered unsafe? by ikegami
in thread Setting signal handlers considered unsafe? by gnosek

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, details, 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, summary, 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.