Приглашаем посетить
Баратынский (baratynskiy.lit-info.ru)

Section 9.4.  Named Arguments

Previous
Table of Contents
Next

9.4. Named Arguments

Use a hash of named arguments for any subroutine that has more than three parameters.

Better still, use named arguments for any subroutine that is ever likely to have more than three parameters.

Named arguments replace the need to remember an ordering (which humans are comparatively poor at) with the need to remember names (which humans are relatively good at). Names are especially advantageous when a subroutine has many optional argumentssuch as flags or configuration switchesonly a few of which may be needed for any particular invocation.

Named arguments should always be passed to a subroutine inside a single hash, like so:


    sub padded {
        my ($arg_ref) = @_;

        my $gap   = $arg_ref->{cols} - length $arg_ref->{text};
        my $left  = $arg_ref->{centered} ? int($gap/2) : 0;
        my $right = $gap - $left;

        return $arg_ref->{filler} x $left
               . $arg_ref->{text}
               . $arg_ref->{filler} x $right;
    }

    
# and then...
for my $line (@lines) { $line = padded({ text=>$line, cols=>20, centered=>1, filler=>$SPACE }); }

As tempting as it may be, don't pass them as a list of raw name/value pairs:

    sub padded {
        my %arg = @_;

        my $gap   = $arg{cols} - length $arg{text};
        my $left  = $arg{centered} ? int($gap/2) : 0;
        my $right = $gap - $left;

        return $arg{filler} x $left
               . $arg{text}
               . $arg{filler} x $right;
    }
    
    # and then...
    for my $line (@lines) {
        $line = padded( text=>$line, cols=>20, centered=>1, filler=>$SPACE );
    }

Requiring the named arguments to be specified inside a hash ensures that any mismatch, such as:


    $line = padded({text=>$line, cols=>20..21, centered=>1, filler=>$SPACE});

will be reported (usually at compile time) in the caller's context:


    Odd number of elements in anonymous hash at demo.pl line 42

Passing those arguments as raw pairs:

    $line = padded(text=>$line, cols=>20..21, centered=>1, filler=>$SPACE);

would cause the exception to be thrown at run time, and from the line inside the subroutine where the odd number of arguments were unpacked and assigned to a hash:

    Odd number of elements in hash assignment at Text/Manip.pm line 1876

It is okay to mix positional and named arguments, if there are always one or two main arguments to the subroutine (e.g., the string that padded( ) is supposed to pad) and the remaining arguments are merely configuration options of some kind. In any case, when there are both positional arguments and named options, the unnamed positionals should come first, followed by a single reference to a hash containing the named options. For example:


    sub padded {
        my ($text, $arg_ref) = @_;

        my $gap   = $arg_ref->{cols} - length $text;
        my $left  = $arg_ref->{centered} ? int($gap/2) : 0;
        my $right = $gap - $left;

        return $arg_ref->{filler} x $left . $text . $arg_ref->{filler} x $right;
    }

    
# and then...
for my $line (@lines) { $line = padded( $line, {cols=>20, centered=>1, filler=>$SPACE} ); }

Note that using this approach also has a slight advantage in maintainability: it sets the options more clearly apart from the main positional argument.

By the way, you or your team might feel that three is not the most appropriate threshold for deciding to use named arguments, but try to avoid significantly larger values of "three". Most of the advantages of named arguments will be lost if you still have to plough through five or six positional arguments first.

    Previous
    Table of Contents
    Next