Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all... Please can someone urgently help me with this piece of code... I get HASH ref error by this piece of code %lang   = %{'lang' . $ARGS{lang}}; Here's the whole sub
sub translate { my %ARGS = ( lang => '', load => 0, @_ ); my ($lang, %lang); %lang = %{'lang' . $ARGS{lang}}; if($ARGS{ttD} && !$ARGS{load}) { $lang = (${$ARGS{ttD}}); } foreach my $key (keys %lang) { $lang->{$key} = $lang{$key}; } return($lang); }
Thanking you all in advance

Replies are listed 'Best First'.
Re: Can't use string ("langLogin") as a HASH ref while "strict refs"
by friedo (Prior) on Jan 23, 2006 at 09:39 UTC
    What exactly are you trying to do? The expression 'lang' . $ARGS{lang} concatinates two strings and you end up with 'langLogin'. You're then trying to use that string as a hash. Why?
      maybe i should have been more descriptive... i have a language file for each of my modules I use in my program... so Login.pm calls the transelate sub like this: &translate(lang=>'Login'); and then basically the translate sub "loads" a hash %langLogin that's in a seperate file called en.pm... Hope this is more descriptive... Thank you for your prompt reply
        Oy. Symbolic references. You really should not do that. A much better strategy would be to use a hash of hashes instead.

        If you're absolutely intent on doing it with symrefs, though, you'll need to turn off strict mode. I reccomend confining the non-strict code to the smallest possible block. Something like this should work.

        my %lang; { no strict 'refs'; %lang = %{'lang' . $ARGS{lang}}; }
Re: Can't use string ("langLogin") as a HASH ref while "strict refs"
by vennirajan (Friar) on Jan 23, 2006 at 09:48 UTC
    Can you give the sample error code ?

    Regards,
    S.Venni Rajan.
    "A Flair For Excellence."
                    -- BK Systems.