in reply to Getting hash of CGI variables

You're just tripping on your shoelace a little. It's not as bad as it seems.

$params = $CGI->Vars; puts a hash reference in $params. The next line of the CGI.pm POD gives the example of dereferencing an example 'key' contained in the hash. $params->{'address'} is only an example of how easy it is to read a param value after you've loaded it into an anonymous hash. The author could just as easily have said $params->{'example_key'}.

Your line that says %params = $CGI->Vars; is wrong, because you're attempting to assign a hash REFERENCE to an actual named hash, rather than to a scalar (scalars hold references). You could modify that line to this:

%params = %{$CGI->Vars};
That dereferences the anonymous hash and assigns it to a real hash, but it doesn't really gain you anything except perhaps one level less of abstraction. And if you do that, you can no longer say $params->{'address'}. You would have to remove the dereferencing operator (->), as in "$params{'address'}".

You probably should have a look at perlreftut, perllol, and perlref to gain a stronger working knowledge of how references work. Then it should all come together for you.


Dave

Replies are listed 'Best First'.
Re: Re: Getting hash of CGI variables
by Lori713 (Pilgrim) on Jan 22, 2004 at 20:59 UTC
    <sarcasm> Uh... thanks for the references to yet more reading material that is often over my head...</sarcasm>

    <great big silly grin>

    Seriously, thanks for the links. It will help me figure out where my knowledge is a bit(?) on the thin side. I've discovered that there are a lot of basics that I don't have, and have unceremoniously ignored in my drive to meet my client's deadline.

    Just in case the more experienced programmers out there read this... it truly is helpful to have links like davido's to help me (and I suspect other newbies) figure out where I/we need to get more of the basics. It's tough when you don't even know what question to ask, or where to look!     :-D

    Thanks for taking the time to help educate me!!

    Lori

      You're certanly welcome.

      I know the POD can be indimidating. I resisted the POD's until after finishing the Llama book and most of the Camel book. But in retrospect, it was a self-imposed resistance; some of them aren't really all that bad.

      In my previous post, I listed the POD's that deal with references, as it seemed that was where you were getting into trouble. I started with perlreftut, which is a tutorial on references. It seems (to me) to be the easiest one to grasp first. I recommended perllol second (discussion of "lists of lists", which is closely related also to hashes of hashes). And third I recommended perlref, which is a lot more involved, in-depth discussion of references. I probably also should have rounded it out with perldsc (Data-structure cookbook). If you take about two hours and read through each of those in approximately that order, references will soon become a familiar friend.

      A suggestion that worked well for me; each day pick a POD and read it. You'll be done in a month or so. And even though at first some will be over your head, by the time you're done you will really be able to put the pieces together, and you'll be surprised at how much of it you start to understand as you read. I need to do it all over again now that I understand most of what's there. ...thanks for reminding me. ;)


      Dave