[Templates] Getting keys from a hash
Karl Dane
karl at rince.net
Tue Feb 26 11:35:59 GMT 2008
Terrific! That did the job.
I've used $cgi->Vars for ages and never had this trouble before. Guess
I'd never tried to do this before.
Thanks everyone for all your help.
Karl
Ash Berlin wrote:
> Vars does crazy tied stuff:
>
>> Many people want to fetch the entire parameter list as a hash in which
>> the keys are the names of the CGI parameters, and the values are the
>> parameters' values. The Vars() method does this. Called in a scalar
>> context, it returns the parameter list as a tied hash reference.
>> Changing a key changes the value of the parameter in the underlying
>> CGI parameter list. Called in a list context, it returns the parameter
>> list as an ordinary hash. This allows you to read the contents of the
>> parameter list, but not to change it.
>
> Try doing the following instead
>
> my $cgihash = { $cgi->Vars };
>
>
> On 26 Feb 2008, at 11:17, Karl Dane wrote:
>
>> Right! So - there's something REALLY weird going on here...
>>
>> To answer Simon, cgihash is from perl's CGI lib: my $cgi = new CGI; my
>> $cgihash = $cgi->Vars;
>>
>> Shouldn't be anything odd about it. But obviously it's behaving
>> differently for some reason.
>>
>> New example. cgihash continues to act as before, but cgihash2 works
>> fine... :
>>
>> cgihash:
>>
>> should tell me this is a hash:
>> [% cgihash %]
>>
>> dump out the contents of the hash:
>> [% USE Dumper %]
>> [% Dumper.dump(cgihash) %]
>>
>> Now give me the keys:
>> [% FOREACH key = cgihash.keys %]
>> [% key %]
>> [% END %]
>>
>> cgihash2:
>>
>> [%
>> cgihash2 = {
>> 'a' => 'newentry',
>> 'newbooking' => '1',
>> 'day' => '06',
>> 'monthdigit' => '02',
>> 'year' => '2008',
>> 'room' => '9',
>> 'gender' => 'male',
>> 'beds' => '3',
>> 'nights' => '2',
>> 'submit' => 'Book'
>> };
>>
>> %]
>>
>> dump out the contents of the hash:
>> [% USE Dumper %]
>> [% Dumper.dump(cgihash2) %]
>>
>> Now give me the keys:
>> [% FOREACH key = cgihash2.keys %]
>> [% key %]
>> [% END %]
>>
>>
>> [[ TEMPLATE ENDS ]]
>>
>> ---
>> Output:
>>
>> cgihash:
>>
>> should tell me this is a hash:
>> HASH(0x8830200)
>>
>> dump out the contents of the hash:
>>
>> $VAR1 = {
>> 'a' => 'newentry',
>> 'newbooking' => '1',
>> 'day' => '06',
>> 'monthdigit' => '02',
>> 'year' => '2008',
>> 'form_FirstName' => 'Charles ',
>> 'form_Name' => 'Bronson',
>> 'form_Address1' => 'home',
>> 'form_Address2' => '',
>> 'form_Address3' => '',
>> 'form_Town' => 'Ambleside',
>> 'form_County' => '',
>> 'form_Postcode' => '',
>> 'form_Country' => 'UK',
>> 'form_Email' => 'david at amblesidebackpackers.co.uk
>> <mailto:'david at amblesidebackpackers.co.uk>',
>> 'form_TelephoneNo' => '',
>> 'room' => '9',
>> 'gender' => 'male',
>> 'beds' => '3',
>> 'nights' => '2',
>> 'submit' => 'Book'
>> };
>>
>>
>> Now give me the keys:
>>
>>
>> cgihash2:
>>
>>
>>
>> dump out the contents of the hash:
>>
>> $VAR1 = {
>> 'monthdigit' => '02',
>> 'a' => 'newentry',
>> 'nights' => '2',
>> 'submit' => 'Book',
>> 'room' => '9',
>> 'newbooking' => '1',
>> 'beds' => '3',
>> 'day' => '06',
>> 'year' => '2008',
>> 'gender' => 'male'
>> };
>>
>>
>> Now give me the keys:
>>
>> monthdigit
>>
>> a
>>
>> nights
>>
>> submit
>>
>> room
>>
>> newbooking
>>
>> beds
>>
>> day
>>
>> year
>>
>> gender
>>
>>
>>
>> [[ TEMPLATE ENDS ]]
>>
>>
>>
>>
>>
>> Clinton Gormley wrote:
>>> On Tue, 2008-02-26 at 11:00 +0000, Karl Dane wrote:
>>>> I guess my example wasn't a very good one - I just wanted to
>>>> demonstrate
>>>> that I was getting _nothing_ when using the keys method. If it had
>>>> worked as expected, then it should have returned something like
>>>> 'ARRAY(0x8a8ca58)'
>>>
>>> ... which is what I get when I try it on my system, with this code:
>>>
>>> [%
>>>
>>> cgihash = {
>>> 'a' => 'newentry',
>>> 'newbooking' => '1',
>>> 'day' => '06',
>>> 'monthdigit' => '02',
>>> 'year' => '2008',
>>> 'room' => '9',
>>> 'gender' => 'male',
>>> 'beds' => '3',
>>> 'nights' => '2',
>>> 'submit' => 'Book'
>>> };
>>>
>>> %]
>>>
>>> should tell me this is a hash:
>>> [% cgihash %]
>>>
>>> dump out the contents of the hash:
>>> [% USE Dumper %]
>>> [% Dumper.dump(cgihash) %]
>>>
>>> Now give me the keys:
>>> [% cgihash.keys %]
>>>
>>> [[ TEMPLATE ENDS ]]
>>> ------------------------------
>>> OUTPUT:
>>>
>>> should tell me this is a hash:
>>> HASH(0x287c4a0)
>>>
>>> dump out the contents of the hash:
>>>
>>> $VAR1 = {
>>> 'monthdigit' => '02',
>>> 'a' => 'newentry',
>>> 'nights' => '2',
>>> 'submit' => 'Book',
>>> 'room' => '9',
>>> 'newbooking' => '1',
>>> 'beds' => '3',
>>> 'day' => '06',
>>> 'year' => '2008',
>>> 'gender' => 'male'
>>> };
>>>
>>>
>>> Now give me the keys:
>>> ARRAY(0x28c7170)
>>>
>>> [[ TEMPLATE ENDS ]]
>>
>> _______________________________________________
>> templates mailing list
>> templates at template-toolkit.org <mailto:templates at template-toolkit.org>
>> http://mail.template-toolkit.org/mailman/listinfo/templates
>
More information about the templates
mailing list