Perl & Symbolic references

They have: 36 posts

Joined: Oct 2004

I would appreciate it if anyone could give me an idiots guide to symbolic references. I understand Hard ref's but symbolic ones just wont fit in my little brain. Ive read explanations in the best known Perl books and all the doc's available on the subject but none are simple enough for me.

If possible could you include information on dereferencing (and maybe how the symbol table fits into the equation).

I know its a hell of alot to ask, but until i thoroughly understand this i cant fully understand the debugging concept of "use strict 'refs'".

TIA

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

A symbolic reference is really just a string that may be in a scalar variable. You're putting another variable's name in a string, which Perl looks up in the symbol table by default, when you try dereferencing it.

An analogy could be web browser bookmarks. When you bookmark a site, your browser normaly stores just the named URL string somewhere, rather than the IP address or indeed whole site. Every time you load the bookmark (like dereferencing), it looks up the URL string via DNS (like a symbol table) to get to the site.

Note that symbolic refs can be problematic much like how bookmarks can be. A bookmark relies on the domain existing and being reachable, since it doesn't refer to the site's content directly. Similarly, a symbolic reference relies on its referent variable existing, and in scope; whereas a hard ref refers to the data without caring about any other symbols.

Since symbolic references aren't as useful or robust as hard references, novices are advised against using symbolic references as a hard ref will usually suffice. Hence the strict refs directive: it will disable symbolic references. That is, Perl will stop interpreting string scalars that way when dereferenced. They'll just be plain string scalars with no extra meaning.

I'm not sure I can explain it with any more detail or clarity without repeating what the books and docs say. No doubt it's a tricky idea, it can be hard to grasp even if you've programmed with references. It's often said by computer science educators that pointers (like refs) and recursion are the two most frequently misunderstood fundamental concepts for students of the subject.

Does that help?

Smiling

They have: 36 posts

Joined: Oct 2004

EXCELLENT, Thanx Abhishek

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.