Re: Combine hash declaration/assignment into single statement?



usenet@xxxxxxxxxxxxxxx wrote:
It is often possible to declare and assign a variable in a single
statement, such as:
my $foo = 'bar';
or
my @foo = qw{bar baz};

Is it possible to do this in a single statement (under strict):

my %character_value;
@character_value{'a'..'z'} = (1..26);



How about using a tied hash? There
may already be a module on CPAN
that does this (I haven't spent the
time to look). Essentially:


package Filmer::Hash;
require Tie::Hash;
@ISA = qw(Tie::StdHash);

sub TIEHASH
{
my ($class, $keys, $values) = @_;
my $self = {};
@$self{ @$keys } = @$values;
bless $self, $class;

}


1;

package main;

use strict;
use warnings;
use Data::Dumper;

tie my %hash, 'Filmer::Hash', [ 'a' .. 'z' ], [ 1 .. 26 ];

print Dumper \%hash;

--
Hope this helps,
Steven

.



Relevant Pages