Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Perl Internals - references and symbol table

by sauoq (Abbot)
on Nov 16, 2002 at 07:12 UTC ( [id://213369]=note: print w/replies, xml ) Need Help??


in reply to Perl Internals - references and symbol table

I assume references are a structure containing a counter of refs, and a pointer.

Internally, variables are associated with their own refcounts. This is true of lexicals as well as global (i.e. package) variables. If you have $variable and later assign \$variable to $reference then the refcount associated with $variable increases. The refcount is not associated with $reference. (Unless, of course, there is a reference to the reference floating around somewhere.) A variable's storage is not deallocated until its refcount has gone to zero.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Perl Internals - references and symbol table
by nothingmuch (Priest) on Nov 16, 2002 at 07:22 UTC
    I assumes lexical refs and dynamic refs are not different, as they are just a means of finding the data and freeing it's space when unused.... What I ment by reference is actually not the referencing part of it, more the resolving to a value. I should have not used the word reference, as it is not correct...

    In terms of my question - I guess i need to be more clear - How are lexical variables found via their name, and does the symbol table -> value path work like I assumes?

    -nuffin
    zz zZ Z Z #!perl

      I think you're conflating the two senses of "reference" here. What we call a "variable" is a name for a structure in memory which contains a C pointer which contains the actual location in memory of the variable's value. [1]. So $user = "diotalevi" has two components - the variable "$user" and the value it points to. Normally we don't ever care about this but if you want more on this you should read Gisle Aas' PerlGuts Illustrated, perlguts, broquaint's Of Symbol Tables and Globs and the other handful of man pages followed by the headers specifically hitting sv.h.

      Reference variables are a variation on that in that they are still variables in the sense I just mentioned. The difference is that instead of pointing to a C array they point to another variable. Perl keeps track of what sort of things a variable points to via a FLAGS bitmask.

      The other difference you seem to hung up on is between package and lexical variables. If you'd read some of the documents I mentioned you'd know that your symbol table is just a hash of reference variables. In that case it's explicitly name->variable. shotgunefx brought up the lexical variables scratch pad. Those AoAs (known as a PADLIST) are pointed to by various code references (glossing on CV for those of you that know better) and while the compiler has the opportunity to convert the uses of the $foo variable from my $foo to a direct usage of the variable in the array (ie, no looking up of the variable by name) some other extensions like PadWalker exploit the fact that the variable's name is kept around (and probably used by things that are beyond my ken) and can go get the various lists of lexical names to create a user-friendly hash. Clearer?

      [1] "Number" variables can store their own value without needing to point to somewhere else.

      __SIG__ use B; printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://213369]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-03-28 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found