but the referrence still exist

Of course. You never changed $arr_r.

Is it same as "Dangling Pointer" in C???

No. A dangling pointer is a pointer to something that doesn't exist anymore. You didn't even try to get rid of the array — undef simply empties it — so there's definitely no dangling pointer.

Even if you tried to get rid of the array (like in the following example), you wouldn't get a dangling. The reference would keep it alive until you got rid of the reference too.

my $ref; { my @array = 'element'; $ref = \@array; } # Because the array is lexically scoped (my), # the array would normally be freed here*, # but the reference is keeping it alive. print "$ref->[0]\n"; # Prints 'element' $ref = undef; # The scalar stops referencing the array, # so it's freed here.

Think of my as new.

my @outer; for (1..2) { my @inner = "pass $_"; push @outer, \@inner; } print "@{ $outer[0] }\n"; # pass 1 print "@{ $outer[1] }\n"; # pass 2

But realize that Perl will get rid of something automatically as soon as noone needs it anymore. For example, if that entire piece of code was in a sub, @outer and the two @inner would get freed by the subroutine exit code*.

One catch: Perl sometimes doesn't realize nothing needs a value anymore. It occurs when you have a reference cycle like the following:

for (1..10) { my $r1; my $r2 = \$r1; my $r3 = \$r2; $r1 = \$r3; } # You have just leaked 30 variables.

* — Aside from optimisations that would only confuse the issue.


In reply to Re: Dangling Pointer::perl referrence by ikegami
in thread Dangling Pointer::perl referrence by Anonymous Monk

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.