Deprecated perl hash reference statement problem

Hello Listers
I've used the following statement in several instances before and never
had any errors reported and the programs all seem to work ok.

%{$hashRef}->{$key} = $value;

But since I've began using....

use strict;
use warnings;
use diagnostics;

in my programs, I'm now starting to get the following error message...
------------------------------------------------------------ ------------------------
Using a hash as a reference is deprecated at
/wpgen/wpgen.cgi line 399 (#1)
(D deprecated) You tried to use a hash as a reference, as in
< %foo-{"bar"} >> or < %$ref-{"hello"} >>. Versions of perl <= 5.6.1
used to allow this syntax, but shouldn't have. It is now deprecated, and
will
be removed in a future version.
wpgen.cgi: Using a hash as a reference is deprecated at /wpgen/wpgen.cgi
line 399.
------------------------------------------------------------ ------------------------
I've tried Googling but have never found any thing that approaches what
could be
used instead of the deprecated statement
Any help would be appreciated.
Thanks
Tony Frasketi



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Tony Frasketi [ Di, 04 Oktober 2005 20:32 ] [ ID #997206 ]

RE: Deprecated perl hash reference statement problem

It depends on what you're trying to do. if $hashRef is a hash
reference, then the correct syntax would be:

$hashRef->{$key} =3D $value;

or

%{$hashRef}{$key} =3D $value;

What you're saying below is something like:

-- Take $hashRef and dereference it to %HASH1 (I'm giving it a name
for clarity)
-- Then use %HASH as a reference to another hash, let's say %HASH2.
-- Finally, use the -> operator to get the $key key of the hash %HASH2.


If this doesn't answer your question, then you will need to give us some
examples.



-----Original Message-----
From: Tony Frasketi [mailto:webmaster [at] spacecovers.com]
Sent: Tuesday, October 04, 2005 11:33 AM
To: beginners [at] perl.org
Subject: Deprecated perl hash reference statement problem

Hello Listers
I've used the following statement in several instances before and never
had any errors reported and the programs all seem to work ok.

%{$hashRef}->{$key} =3D $value;

But since I've began using....

use strict;
use warnings;
use diagnostics;

in my programs, I'm now starting to get the following error message...
------------------------------------------------------------ ------------
------------
Using a hash as a reference is deprecated at


<snip>



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Timothy Johnson [ Di, 04 Oktober 2005 20:37 ] [ ID #997207 ]

Re: Deprecated perl hash reference statement problem

On Oct 4, Tony Frasketi said:

> I've used the following statement in several instances before and never had
> any errors reported and the programs all seem to work ok.
>
> %{$hashRef}->{$key} = $value;

%hash->{$key} and [at] array->[$idx] are syntaces that you should not use.
The fact that they work is due to intricacies of the Perl parser.
Similarly, %{$hashref}->{$key} and [at] {$arrayref}->[$idx] are equally bad.

Use $hash{$key} and $array[$idx] for normal hash and array access. When
using hash and array references, you have two choices: $hash->{$key} and
$array->[$idx], or $$hash{$key} and $$array[$idx].

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://www.perlmonks.org/ % have long ago been overpaid?
http://princeton.pm.org/ % -- Meister Eckhart

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
japhy [ Di, 04 Oktober 2005 20:44 ] [ ID #997208 ]

Re: Deprecated perl hash reference statement problem

Timothy Johnson wrote:

>It depends on what you're trying to do. if $hashRef is a hash
>reference, then the correct syntax would be:
>
> $hashRef->{$key} = $value;
>
>or
>
> %{$hashRef}{$key} = $value;
>
>What you're saying below is something like:
>
>-- Take $hashRef and dereference it to %HASH1 (I'm giving it a name
> for clarity)
>-- Then use %HASH as a reference to another hash, let's say %HASH2.
>-- Finally, use the -> operator to get the $key key of the hash %HASH2.
>
>
>If this doesn't answer your question, then you will need to give us some
>examples.
>
>
>

Thanks for the quick response, Timothy. Here is the function in question
and a representative calling sequence....

Calling sequence...............
# Form buffer
my(%FIELDS,$query,$code,$checks);

# Get data from form (get_form_data_1 is local to this program) !!!!
&get_form_data_1(\%FIELDS);

Function........................
sub get_form_data_1 {
my($hashRef) = [at] _;
my($buffer) = "";
my($key,$value,$pair, [at] pairs);

if ($ENV{'REQUEST_METHOD'} eq "GET") {
$buffer = $ENV{'QUERY_STRING'};
}else {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}
[at] pairs = split(/&/, $buffer);
foreach $pair ( [at] pairs) {
my($key, $value) = split(/=/, $pair);
$key = decodeURL($key); # Decode the key
$value = decodeURL($value); # Decode the value
%{$hashRef}->{$key} = $value;! # Enter the value in the hash
<----- Deprecated stmt
print "get_form_data_1: Setting $key to [$value]<br>"; #Debug....
}
}

Thanks
Tony



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Tony Frasketi [ Di, 04 Oktober 2005 20:54 ] [ ID #997209 ]

Re: Deprecated perl hash reference statement problem

On Oct 4, Tony Frasketi said:

> sub get_form_data_1 {
> my($hashRef) = [at] _;
> my($buffer) = "";
> my($key,$value,$pair, [at] pairs);
>
> if ($ENV{'REQUEST_METHOD'} eq "GET") {
> $buffer = $ENV{'QUERY_STRING'};
> }else {
> read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
> }
> [at] pairs = split(/&/, $buffer);
> foreach $pair ( [at] pairs) {
> my($key, $value) = split(/=/, $pair);
> $key = decodeURL($key); # Decode the key
> $value = decodeURL($value); # Decode the value
> %{$hashRef}->{$key} = $value;! # Enter the value in the hash <-----
> Deprecated stmt
> print "get_form_data_1: Setting $key to [$value]<br>"; #Debug....
> }
> }

Blech! Please, please, PLEASE use CGI.pm for your form-parsing needs.
It's standard and it works, and it handles things like multiple-value
select-boxes (which get_form_data_1() does not).

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://www.perlmonks.org/ % have long ago been overpaid?
http://princeton.pm.org/ % -- Meister Eckhart

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
japhy [ Di, 04 Oktober 2005 21:07 ] [ ID #997212 ]

RE: Deprecated perl hash reference statement problem

>Timothy Johnson wrote:
>
>>It depends on what you're trying to do. if $hashRef is a hash
>>reference, then the correct syntax would be:
>>
>> $hashRef->{$key} =3D $value;
>>
>>or
>>
>> %{$hashRef}{$key} =3D $value;
>>
>>What you're saying below is something like:
>>
>>-- Take $hashRef and dereference it to %HASH1 (I'm giving it a name
>> for clarity)
>>-- Then use %HASH as a reference to another hash, let's say %HASH2.
>>-- Finally, use the -> operator to get the $key key of the hash
%HASH2.
>>
>>
>>If this doesn't answer your question, then you will need to give us
some
>>examples.
>>
>
>Thanks for the quick response, Timothy. Here is the function in
question
>and a representative calling sequence....
>
>Calling sequence...............
># Form buffer
>my(%FIELDS,$query,$code,$checks);

># Get data from form (get_form_data_1 is local to this program) !!!!
>&get_form_data_1(\%FIELDS);

>Function........................
>sub get_form_data_1 {
> my($hashRef) =3D [at] _;
>

<snip>

> %{$hashRef}->{$key} =3D $value;! # Enter the value in the hash
><----- Deprecated stmt
> print "get_form_data_1: Setting $key to [$value]<br>"; #Debug....
> }
>}

It looks like the syntax you are looking for is

$hashRef->{$key} =3D $value;


When you use the -> operator, you are indirectly accessing the value for
the hash POINTED TO by the scalar on the left side. So you're basically
saying that

$hashRef->{$key}

is the key "$key" of the hash that $hashRef is pointing to.


FYI, I should correct the typo in my first email. If you want to use
the %{$hashRef} syntax, then it should be:

${$hashRef}{$key}

NOT

%{$hashRef}{$key}




--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Timothy Johnson [ Di, 04 Oktober 2005 21:11 ] [ ID #997213 ]

Re: Deprecated perl hash reference statement problem

Jeff 'japhy' Pinyan wrote:

>
> %hash->{$key} and [at] array->[$idx] are syntaces that you should not use.
> The fact that they work is due to intricacies of the Perl parser.
> Similarly, %{$hashref}->{$key} and [at] {$arrayref}->[$idx] are equally bad.
>
> Use $hash{$key} and $array[$idx] for normal hash and array access.
> When using hash and array references, you have two choices:
> $hash->{$key} and $array->[$idx], or $$hash{$key} and $$array[$idx].

Thanks Japhy
Both $hash->{$key} and $$hash{$key} work fine and this also works
Although I'm not sure why.... ${$hash}{$key}.

I seem to have a lot of problems with the perl language in that there
are so many ways to accomplish (express) the same thing.... A wonderful
feature but sometimes confusing as hell (to me at least).

Anyway thanks again - Problem solved! No more error messages.
Best regards
Tony Frasketi



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Tony Frasketi [ Di, 04 Oktober 2005 21:19 ] [ ID #997214 ]

Re: Deprecated perl hash reference statement problem

On Oct 4, Tony Frasketi said:

> Both $hash->{$key} and $$hash{$key} work fine and this also works Although I'm
> not sure why.... ${$hash}{$key}.

The reason ${$hash}{$key} works is because it's a generalization of
$$hash{$key}. The rule of thumb is:

1. start with $HASH{key}
2. replace HASH with a block: ${ ... }{key}
3. fill the block with a hash reference: ${$hashref}{key}
4. if the contents are simple enough, braces are not needed

> I seem to have a lot of problems with the perl language in that there are so
> many ways to accomplish (express) the same thing.... A wonderful feature but
> sometimes confusing as hell (to me at least).

Consider reading 'perldoc perlreftut', a tutorial to using references.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://www.perlmonks.org/ % have long ago been overpaid?
http://princeton.pm.org/ % -- Meister Eckhart

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
japhy [ Di, 04 Oktober 2005 21:16 ] [ ID #997215 ]

RE: Deprecated perl hash reference statement problem

Tony Frasketi wrote:

>Both $hash->{$key} and $$hash{$key} work fine and this also works
>Although I'm not sure why.... ${$hash}{$key}.

There is a reason for this. Let's say you had two variables:

%hash (a hash)
$hash (a hash reference)

You could confuse the interpreter if you wrote this:

$$hash{key};

Is that the key of the hash that $hash points to, or a dereference of a
scalar reference contained in the key of %hash? To be more specific,
you can use this syntax.

${$hash{key}} (the scalar referenced by $hash{$key})

${$hash}{$key} ($the key of the hash pointed to by $hash)

I usually leave the brackets in so I don't have to worry about it.


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Timothy Johnson [ Di, 04 Oktober 2005 21:21 ] [ ID #997216 ]

Re: Deprecated perl hash reference statement problem

Thanks to all who answered - I really appreciate the help!
Now busy reading perldoc perlreftut !
Tony

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Tony Frasketi [ Di, 04 Oktober 2005 21:45 ] [ ID #997217 ]
Perl » gmane.comp.lang.perl.beginners » Deprecated perl hash reference statement problem

Vorheriges Thema: STAT function; limit a log file.
Nächstes Thema: Need help with Perl Book