Named Parameters: Additional Information - Page 13
July 13, 2001
Before leaving the subject of named parameters, it is worth
briefly mentioning the Alias module, available from
CPAN. Alias provides the subroutines
alias and attr, which generates aliases
from a list of key-value pairs. Both subroutines use typeglobs to
do the job.
The alias subroutine takes a list of key-value pairs
as its argument, and is therefore suited to subroutines. The type
of variable defined by the alias is determined by the type of
value it is aliased to; a string creates a scalar, a list creates
an array. Here is yet another volume subroutine that
uses alias:
#!/usr/bin/perl
# volalias.pl
use warnings;
use strict;
no strict 'vars';
use Alias;
# subroutine using 'alias'
sub volume {
alias @_;
return $height * $width * $length;
}
# a call to the subroutine
print volume(height => 1, length => 9,
color => 'red', width => 4);
# aliased variables visible here
print " = $height x $width x $length \n";
[Lines 16 and 17 above are one line. They have been split for
formatting purposes.]
However, alias suffers from three serious
deficiencies. The first is that it is not compatible with
strict vars; if we want strict variables we will
have to declare all the aliased variables with use
vars or (preferably) our. Another is
that alias creates global aliases that persist
outside the subroutine, which is not conducive to good
programming. The third is that if we only use the variable once
we'll get a warning from Perl about it. The script above does not
do that because of the last line. Comment out that line, and all
three variables will generate used only once warnings.
attr takes a reference to a hash and creates aliases
based on the keys and values in it. attr $hashref is
similar to alias %{$hashref}, but localizes the
aliases that it creates. It is ideal to use with object methods
for objects based around hashes since each object attribute
becomes a variable (hence the name):
#!/usr/bin/perl
# attr.pl
use warnings;
use strict;
{
package Testing;
use Alias;
no strict 'vars'; # to avoid declaring vars
sub new {
return bless {
count => [3, 2, 1],
message => 'Liftoff!',
}, shift;
}
sub change {
# define @count and $message locally
attr(shift);
# this relies on 'shift' being a hash reference
@count = (1, 2, 3);
$message = 'Testing, Testing';
}
}
my $object = new Testing;
print "Before: ", $object->{'message'}, "\n";
$object->change;
print "After : ", $object->{'message'}, "\n";
print $Testing::message, "\n";
# warning - 'attr' vars do not persist
close Testing::count;
We can also define 'constants' with the const
subroutine. This is actually just an alias for alias
(it's even defined using alias inside the module,
and must be imported explicitly:
# const.pl
use Alias qw(const);
# add 'alias' and/or 'attr' too, if needed
const MESSAGE => 'Testing';
print $MESSAGE, "\n";
Attempting to modify the value of a constant produces an error:
# ERROR: produce 'Modification of a read-only
# value attempted at ...'
$MESSAGE = 'Liftoff!';
The Alias module also provides
several customization features, mainly for the attr
subroutine, which allows us to control what gets aliased and how.
Refer to 'perldoc Alias' for a rundown and some more
examples.
Named Parameters - Page 12
Professional Perl Programming
Prototypes - Page 14
|