Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Pseudo-hashes deprecated

by SavannahLion (Pilgrim)
on Oct 17, 2004 at 07:33 UTC ( [id://399893] : perlquestion . print w/replies, xml ) Need Help??

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

I'm using to experiment with some Perl code. In the documentation it explains that you can create loops with repeating values. Here is the relative section I'm talking about for those who don't have
The <TMPL_LOOP> tag is a bit more complicated than <TMPL_VAR>. The <TMPL_LOOP> tag allows you to delimit a section of text and give it a name. Inside this named loop you place <TMPL_VAR>s. Now you pass to param() a list (an array ref) of parameter assignments (hash refs) for this loop. The loop iterates over the list and produces output from the text block for each pass. Unset parameters are skipped. Here's an example: In the template: <TMPL_LOOP NAME=EMPLOYEE_INFO> Name: <TMPL_VAR NAME=NAME> <br> Job: <TMPL_VAR NAME=JOB> <p> </TMPL_LOOP> In the script: $template->param(EMPLOYEE_INFO => [ { name => 'Sam', job => 'progra +mmer' }, { name => 'Steve', job => 'soda + jerk' }, ] ); print $template->output(); The output in a browser: Name: Sam Job: programmer Name: Steve Job: soda jerk As you can see above the <TMPL_LOOP> takes a list of variable assignments and then iterates over the loop body producing output.
Alright. Not a problem. Building the specific Perl code, and implementing it as described causes no problems.
So I wanted to learn more about how this code worked so I kept reading through the file and experimenting.

Apparently, I'm not having much luck in understanding this. I think it's either because the only time I have to play with Perl for the last several weeks is between 10:00 PM and 1:00 AM, or the total program is just so large and complex, so my poor brain just can't wrap itself around this.

Whenever I experiment with this, I always get a "Pseudo-hashes deprecated" message. I even found a very similar example, but running that example yields an identical error message. Unfortunately, I think there was more to that example, but I lost my place in the PM search and haven't found it yet.

It looked something like:

my $ph = [ { alpha => 1, beta => 2, gamma => 3, delta => 4, epsilon => + 5 }, #hashref "val a", "val b", "val c", "val d", "val e" ]; + # maps to these values # Print some of the elements print $ph->[1], "\n"; print $ph->{ alpha }, "\n"; print $ph->{ epsilon }, "\n";
I'm betting that once I type this, I'll kick myself in the butt for the obvious answers that will likely ensue, especially since the message is painfully obvious that I shouldn't use that method. But I keep seeing goblins in the corner... or is that my cat stalking me? :-(

How is it that when I try to copy this method, I get that such a blatant warning, but gets away with using it?

Thanks for your patience.
Prove your knowledge @ HLPD

Replies are listed 'Best First'.
Re: Pseudo-hashes deprecated
by ikegami (Patriarch) on Oct 17, 2004 at 09:23 UTC

    A quick search of CPAN turned up Class::PseudoHash which explains:

    Due to its impact on overall performance of ordinary hashes, pseudo-hashes are deprecated in perl v5.8, and will cease to exist in perl v5.10. By then, the fields pragma is supposed to change to use a different implementation.

    Class::PseudoHash transparently supports PseudoHashes by simply adding two lines to your program. Now, if you're writting new code, you probably want to avoid using this.

    I have no experience with this module.

Re: Pseudo-hashes deprecated
by jdalbec (Deacon) on Oct 17, 2004 at 19:00 UTC
    $template->param(EMPLOYEE_INFO => [ { name => 'Sam', job => 'progra +mmer' }, { name => 'Steve', job => 'soda + jerk' }, ] );
    The code is using an array of hashes, but it's not a pseudo-hash since the values ('Sam' and 'programmer') in the first hash in the array are not positive integers.
    print $ph->{ alpha }, "\n"; print $ph->{ epsilon }, "\n";
    If you want to get rid of the warnings here, you could implement pseudo-hash lookups "by hand":
    print $ph->[$ph->[0]{alpha}], "\n"; print $ph->[$ph->[0]{epsilon}], "\n";
    Update: You might be getting the warning in your experiments because you have $AoH->{...} instead of $AoH->[...]{...}. Maybe you could post some of the code near the line that appears in the warning message.
Re: Pseudo-hashes deprecated
by BUU (Prior) on Oct 17, 2004 at 09:12 UTC
    It's fairly simple. What you are trying to there is use a pseudo-hash. Pseudo-hashes are deprecated and should not be used. Simple. Possibly whatever code you are copying from was written before pseudo hashes were deprecated.

    (Pseudo hashes are when you create an array with the first element being a hash ref and attempt to use the array as both an array an a hash, as you attempt to do).