Bootstrap FreeKB - Perl (Scripting) - return a hash
Perl (Scripting) - return a hash

Updated:   |  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.

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 Buy Me A Coffee


Add a Comment

Please enter 2b00c1 in the box below so that we can be sure you are a human.