Non Existing Hash Key and Perl Function
Using Perl Hashes – Part 5
Perl Course
Foreword: In this part of the series I explain the problem of passing a non-existing hash key to a Perl function and how to solve it.
By: Chrysanthus Date Published: 3 Nov 2015
Introduction
Passing a Non-Existing Hash Key to a Perl Function
Read and try the following code:
use strict;
my %ha;
sub fn
{
$_[0] = 'aa';
}
fn($ha{one});
print $ha{one};
There should have been no output printed, but the output is,
aa
This is wrong. The hash had an empty list. However, as the assumed key was passed to the function, and the assumed key as $_[0] being assigned a value, the hash developed the key/value pair. It does not have to be like that. That is a weakness. See solution below.
Passing a Non-Existing Hash Slice
If you pass a non-existing hash slice, even if there is no assignment in the function body, the hash develops the keys. Try the following code:
use strict;
my %ha;
sub fn
{
}
fn(@ha{'one', 'two'});
print "Hash now has a key as one\n" if exists($ha{'one'});
print "Hash now has a key as two\n" if exists($ha{'two'});
The output is:
Hash now has a key as one
Hash now has a key as two
This output is wrong. It means the hash has developed keys when it was not supposed to; just because a slice with the assume keys has been passed to a function. See solution below.
This module that comes with Perl, can be used to stop addition of more keys into a hash. It can be used to stop more keys, but allow a list of possible names (keys) that can be added. You bring in the module to the program as follows:
use Hash::Util qw(
hash_seed all_keys
lock_keys unlock_keys
lock_value unlock_value
lock_hash unlock_hash
lock_keys_plus hash_locked
hidden_keys legal_keys
);
You can then restrict the keys to the current keyset for a particular hash as follows:
lock_keys(%hash);
The hash may not be empty; you can restrict it to possible keys of a keyset as follows:
lock_keys(%hash, @keyset);
If there are already some keys, you can restrict it to possible additional keys as follows:
lock_keys_plus(%hash, @additional_keys);
Try the following code and note that the assumed hash key could not be added to the hash, resulting from the function call (and assignment).
use strict;
use Hash::Util qw(
hash_seed all_keys
lock_keys unlock_keys
lock_value unlock_value
lock_hash unlock_hash
lock_keys_plus hash_locked
hidden_keys legal_keys
);
my %ha;
lock_keys(%ha);
sub fn
{
$_[0] = 'aa';
}
fn($ha{one});
print $ha{one};
I tried the code and I had the following output (error message):
Attempt to access disallowed key 'one' in a restricted hash at C:\temp.pl line 19.
This means that the key was not created as hoped.
Try the following code for a slice and function, and note that there is no creation of keys:
use strict;
use Hash::Util qw(
hash_seed all_keys
lock_keys unlock_keys
lock_value unlock_value
lock_hash unlock_hash
lock_keys_plus hash_locked
hidden_keys legal_keys
);
my %ha;
lock_keys(%ha);
sub fn
{
}
fn(@ha{'one', 'two'});
print "Hash now has a key as one\n" if exists($ha{'one'});
print "Hash now has a key as two\n" if exists($ha{'two'});
I tried the code and I had the following output (error message):
Attempt to access disallowed key 'one' in a restricted hash at C:\temp.pl line 22.
This means that non of the assumed keys from the hash slice was created.
That is it, for this part of the series.
Chrys
Related Links
Perl BasicsPerl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course
BACK