in reply to Re: Dangling Pointer::perl referrence
in thread Dangling Pointer::perl referrence

Hi ikegami,

Thanks for your reply. From the example you given, it means that the referrence will keep the array alive unless I destroy the referrence too (as below)

my $ref; { my @array = 'element'; $ref = \@array; }
But what if I use like below
{ my @array = 'element'; } $ref = \@array; print "$ref\n";

OR

$ref=\@arr; #### @arr doesn't excist at all print "$ref\n";
In both this case I get a output like below

ARRAY(0x1830614)

Now where it's referring too? There is no such variable ... means no such memory location?

Thanks

Replies are listed 'Best First'.
Re^3: Dangling Pointer::perl referrence
by ikegami (Patriarch) on Feb 18, 2010 at 01:46 UTC

    { my @array = 'element'; } $ref = \@array; print "$ref\n";

    That doesn't compile, at least not under use strict; which you should be using. The reason is the second @array is a completely different variable than the first. It's package variable $main::array, aka a global variable. Global variables, by definition and by design, don't get freed before the program exits (without taking some intentional action to remove it from the symbol table). Think of extern int a; in a .h.

    Now where it's referring too? There is no such variable ... means no such memory location?

    Global variables get created simply by using them. By taking a reference to previously non-existant @main::array (2nd snippet) or @main::arr (3rd snippet), they get created. Well, technically, they already existed in those snippets because the parser created them when it noticed they will be used. The following code peeks at the symbol table where package variables reside:

    >perl -E"say *array{ARRAY}||0;" 0 >perl -E"say *array{ARRAY}||0; @array; say *array{ARRAY}||0;" ARRAY(0x182a24c) ARRAY(0x182a24c) >perl -E"say *array{ARRAY}||0; @{'array'}; say *array{ARRAY}||0;" 0 ARRAY(0x238b24)

    The parser created @main::array when it compiled @array
    The runtime created @main::array when it evaluated @{"array"}

Re^3: Dangling Pointer::perl referrence
by jethro (Monsignor) on Feb 18, 2010 at 00:14 UTC

    Different to C there are global variables that spring into existence the moment you refer to them. So when perl executes $ref = \@array; in your second example, @array is a new global (but empty) variable that has nothing in common with the 'my @array' variable except for the name.

    If you had a line 'use warnings;' at the beginning of your script (a practice very recommended), you would have seen the following warning: "Name "main::array" used only once: possible typo at ./t7.pl line 6". Ok, not that easy to decipher, but once you know that global variables are living in name spaces, the default one being called 'main::', you would have a clue about what went wrong

    If you also had a line 'use strict;' at the beginning of your script (a practice very recommended for any script larger than a few lines) your script would have aborted at the compilation stage with error messages about global variables.