Perl (Scripting) - return a hash

by
Jeremy Canfield |
Updated: March 26 2021
| Perl (Scripting) articles
If you are not familiar with subroutines, check out our article on Perl - Getting Started with Subroutines.
Let's say you have the following script. In this example, %hash in the foo subroutine contains a key value pair of employee => John Doe, and then %hash is returned.
#!/usr/bin/perl
use strict;
use warnings;
sub foo {
my %hash = ( employee => 'John Doe' );
return %hash;
}
Outside of the subroutine, the output of the greeting subroutine is stored in the %foo hash.
my %foo = greeting();
Dumper can be used to display the structure of the hash.
use Data::Dumper;
print Dumper \%foo;
The %foo hash contains the employee key with a value of John Doe.
$VAR1 = {
'employee' => 'John Doe'
};
Did you find this article helpful?
If so, consider buying me a coffee over at