References are needed when programming properly in OO fashion, OO Perl Modules. They are a great thing. It might take a few weeks before you understand very well what is going on (for those new to perl). Once you do you will embrace it.
Nick -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Geoff Sent: Thursday, June 26, 2008 10:12 AM To: [EMAIL PROTECTED] Cc: [email protected] Subject: Re: variables Perl references are really cool things, you just need to play with the concept to 'get' it. Declare a variable. Say it's $foo. Set $foo = 'bar'; Now if you print "$foo"; you'll see "bar" printed out. I'm not going to show you this; I assume you already know. Now try this: C:\>perl $foo='bar'; $ref = \$foo; print "$ref\n"; print "$$ref\n"; When you run the program, you'll see something like this: SCALAR(0x325bc0) bar You see, $ref is set to the value of the space in memory where Perl is storing the value of $foo. The \ sigil tells Perl to return the address where the $foo variable is stored. So when you print out $ref, you get the Perl-ish representation of "where $foo is". The final line of the code is interesting: it says: give me the SCALAR that is located where you are storing $foo. That's the double $ at work there. The first $ sigil is telling Perl how to interpret the value at SCALAR(0x325bc0). Now let's talk about your particular issue. Your subroutine is returning a reference, this time to a hash. Imagine for a moment that internally, the parse_content_type() sub is methodically doing whatever it does, and placing the results into %hash. At the end of the sub, there is a line that looks like this: return(\%hash); The \ sigil is telling Perl: give me the address where %hash is located. (It looks something like this when represented as a scalar: HASH(0x2735bc0) -- see the difference from the SCALAR in the example above? Internally, Perl labels its data structures so it can tell how to interpret them.) Now, here you are with $data being set to \%hash. What to do? You "dereference" $data to get at the info stored in %hash. There are *at least* two ways to do that. ONE way is to say $data->{somekey}. When Perl sees the $ sigil and the ->, it knows that you're telling it: give me the 'somekey' value stored in the hash that is located at $data. Here's an example: C:\>perl my %hash; $hash{'key'} = 'value'; $hashref = \%hash; print $hashref->{'key'}."\n"; When you run the program, you'll see this: value ANOTHER way is to say: %myhash = %$data, and then %myhash becomes the equivalent of the original %hash. A short example will illustrate this best: C:\>perl my %hash; $hash{'key'} = 'value'; $hashref = \%hash; %anotherhash = %$hashref; print $anotherhash{'key'}."\n"; When you run the program, you'll see this: value Why? The % sigil is the magic. When you assign %anotherhash to %$hashref, you're essentially saying: assign %anotherhash to the values represented in the hash stored at the memory location $hashref. So you can either use the -> dereferencing scheme, or the %$ dereferencing scheme, according to taste. It is functionally the same. I do encourage you to PLAY WITH THIS. It only makes sense when you do. After you've got the concept down, you'll wonder what was so hard about it! :-)) Best, -- --Geoff Cell: (313)506-7295 Sterling Heights CERT '04 Grand Cherokee | '05 Town & Country http://www.moparmailinglist.com _______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs This email is intended for the recipient only. If you are not the intended recipient please disregard, and do not use the information for any purpose. _______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
