Firstly, I would just do this with the intermediate value. But, you have two separate more advanced options. I don't suggest /either/ of these if you're new to perl - in fact I rather like the intermediary value solution the best, but for completeness:
- You can use Tie::Hash which allows you to make an object accessible with hash syntax and hash style dereferencing (
->{key1}
), you can find more information on this method with perldoc perltie
, and the man page linked above for Tie::Hash
;
- You can abandon the idea of using a hash; and, use an object, or even Moose which will allow you to initialize the object from a hash, sharing the same value. I don't see much purpose in a real world example in doing this.
Here is a Moose example.
package Class;
use Moose;
has [qw/key1 key2/] => ( isa => 'Str', is => 'rw', init_arg => 'key1' );
my $o = Class->new( { key1 => 'foobar' } );
print $o->key1, $o->key2;