Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: O, the horrors of references and complex data structures

by goldclaw (Scribe)
on Feb 06, 2001 at 03:04 UTC ( [id://56543]=note: print w/replies, xml ) Need Help??


in reply to O, the horrors of references and complex data structures

OK, I won't actualy recomend this, as its higly magical, and it is not very intuitve what you are doing.(Id hate to maintain code like this....)

I'll also call you globalHash globalHash if you dont mind. You'll understand why...

Anyway, the globalHash is stored in something called a glob. Now a glob is sort of a hash, with the values beeing references to everything that can be called globalHash. So the %globalHash is actually stored as a reference to a hash in the *globalHash glob. (There is also a refernce to a list, in case you define @globalHash. Since you probably haven't that refernce is probably undef in your program) Now, to set %globalHash to a new hash, simply set the glob to an empty hash reference like this:

push %AoH, \%globalHash; *globalHash={};
That sets the hash part of the glob to a new, empty hash reference, so that %globalHash is now empty. The good thing is that a glob works a lot like a hash table, so the old hash you had stored there is not gone. There are still references to it in your AoH list. Its just not possible to get at it through the globalHash anymore. Magic, huh...

Anyway, by assigning a scalar reference(number, string etc) to the *globalHash glob, you will change the scalar part in the glob. Assigning a list reference will change the list part of it. Note also that this won't work for variables you have declared with my as those don't use glob's.

goldclaw

Replies are listed 'Best First'.
Re: Re: O, the horrors of references and complex data structures
by DeusVult (Scribe) on Feb 06, 2001 at 21:38 UTC
    Note also that this won't work for variables you have declared with my as those don't use globs.

    Forgive me if I'm being stupid, but when I fail to declare a variable with my, I get a compile error and my script won't run. So this technique must be even more magical than you let on :)

    Some people drink from the fountain of knowledge, others just gargle.

      Well, my isn't the only way to declare a variable and stop use strict 'vars' from complaining. Other ways include use vars, local and using fully qualified variable names (e.g. $main::foo).

      In general, the difference between package variables (that live in a typeglob) and lexical variables (which don't) is a very deep magic that lies at the heart of a thorough knowledge of Perl. I recommend a close study of the Variables section of the Camel book (3ed).

      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found