An elegant way to mix and match hash entries is by using the technique of hash slices. For example, let's just say that you have defined the following hash:
my $config = {
firstname => 'Joseph',
lastname => 'Smith',
user => 'pickle',
...
database => 'fruits',
password => 'z4KLf3&3z',
...
host => 'zeta.demon.nl',
};
Suppose that you are only interested in the hash-keys related to the database stuff. Then you can use the following fancy one-liner:
my ($dbhost, $dbdatabase, $dbuser, $dbpassword) =
@$config{ qw( host database user password ) };
As the saying goes, there is always more than one way to do it with Perl. Pretty neat stuff, wouldn't you say?
Recent Comments
- Charles
- jpmcfarlane
- Kiffin
- jpmcfarlane
- KathleenC