#!/usr/local/bin/perl use strict; use warnings; package MyData; sub getData { my $data = { 'test' => 'yes', 'key' => 'value', 'some' => 'thing', 'array' => [ 1, 2, 3 ], }; my $key = shift; defined $data->{$key} ? return $data->{$key} : return; } package main; foreach my $testcase (qw(test key some bork)) { my $value = MyData::getData($testcase); if ($value) { print "Data for $testcase : $value \n"; } else { print "No such data : $testcase .\n"; } }