in reply to Returning A Pseudo-Hash in Array Context

First: "unshift @retval, \@names;" only works if @names = ( a => '1', b => '2', etc);

Second: "That is, I can invoke the power of the pseudo-hash, or ignore it and use the list."
True, if you remember to throw out the first value when you're just using it as a list. Can you be sure that you or whoever inherits this code will always do that?

Third: If I've got this right, one of the key things about psuedohashes is that unless you create them as a typed lexical variable (e.g. 'my MyGMTime $time;') AND have a 'use fields qw(all my fields in proper order)' in your package, then the pseudohash conversion (taking what looks like hashrefs but converting them into arrayrefs) will be done at run time, not compile time. The result still gives you the easier, hash-style interface, but it's slower than either a straight array or a hash. Doing it at compile time is nice, but you have to jump through these hoops.

My advice: If you only want mnemonic accessors while maintaining an array structure, return a blessed array and create a generic "get" accessor that maps strings to positions. Here's what it would look like:

package MyGMTime; # MUST be ordered correctly: use enum qw(sec min hour mday mon year wday yday); sub new { my ($class) = shift; my @values = gmtime(); bless [@values], $class; } sub get { my ($self, $field) = @_; no strict 'refs'; return $self->[&$field]; } package main; my $x = MyGMTime->new; print $x->get("year"), "\n"; # mnemonic/hashly print $$x[5], "\n"; # arrayish
Or, if you don't want to use enum.pm:
### use this instead of the 'use enum' statement: my %index = ( sec => 0, min => 1, hour => 2, mday => 3, mon => 4, year => 5, wday => 6, yday => 7, ); ### Replace get() with this. All remaining code is identical. sub get { my ($self, $field) = @_; return $self->[$index{$field}]; }

-- Frag.