When $x and $y aren't initialized, there is no functional difference. The difference is only noticeable when they've been previously initialized.

my ( $x, $y ); $x = \12; $$y = 13; my $a = $x; my $b = $y; $x = \14; $$y = 15; print("a = $$a\n"); # 12 print("b = $$b\n"); # 15 print("x = $$x\n"); # 14 print("y = $$y\n"); # 15

$x = \14 create a reference to the constant 14. It then stores the reference in $x, erasing $x's previous content.

$$y = 15 replaces the value of the variable at which $y points with 15. In my example, $b points to that same variable. When $y isn't initialized, a new variable is created through auto-vivification. In languages without auto-vivification (C, C++ and Java, for example), you get a NULL pointer error instead. In languages with auto-vivification, you just created a new variable, possibly by accident.

In C++ parlance, $x = \14 is similar to

x = new int; *x = 14;

while $$y = 15 is similar to

if (y == NULL) { y = new int; } *y = 15;

In reply to Re: how do $x = \12 differ from $$y = 12 ? by ikegami
in thread how do $x = \12 differ from $$y = 12 ? by borisz

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.