in reply to Dangling Pointer::perl referrence
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.
|
|---|