No, it's not a dangling pointer. I'm not entirely sure references (pointers) can be made to dangle in Perl; I suppose it could be done if someone wanted to try hard enough. But Perl has automatic garbage collection.

This little demo might prove interesting:

no strict; # just for this demo use warnings; my $ref = \do{ my $anon = 42 }; my $deref = ${ $ref }; print '$ref: ' , "$ref\n"; print '$deref: ' , "$deref\n"; print '$anon: ' , "$anon\n"; __END__ Output: Name "main::anon" used only once: possible typo at ./anon-scalar-ref-d +emo.pl line 9. $ref: SCALAR(0x9dbefd8) $deref: 42 Use of uninitialized value $anon in concatenation (.) or string at ./a +non-scalar-ref-demo.pl line 9. $anon:

Explanation:

I encourage you to copy this demo and fool around with it until you feel more comfortable with references. Suggest changing the assignment, eliminating the do-block, taking a reference to $ref, and dropping braces in various places to force scope. (One experiment at a time!) Here's a few other changes to try:

my $ref     = \do{ @arr = (1, 2, 3) };

or:

my @arr = (1, 2, 3); my $ref = \@arr;

or:
my $ref      = \(1, 2, 3);

or:
my $ref      = [1, 2, 3];

Can you guess for which of the above print @arr; will not raise a warning?

Strongly suggest you try perldoc perlref, which goes into much greater detail. Mark Dominus' perldoc perlreftut is an easy introduction.


In reply to Re: Dangling Pointer::perl referrence by Xiong
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.